Python Chalice使ってみました

AWSAPI Gateway/Lambdaを使う便利なツール「Chalice」を使ってみました。 なんのためにIAMを使うのかその意味が少しわかった気がします。

素晴らしい!

Crowller on AWSに役立てたい。

Spring Bootの始め方

Spring (Boot)関連の書籍も大分増えてきましたが、私のおすすめは以下の手順です。

※)Springの仕様(機能)は膨大なので一度に全てを学習するのではなくある程度開発力が身についたら実践しながら覚えていくしかないと思います。

書籍はこの本だけでOKだと思います。 ただし、少し古い本なので最新情報を検索して補う必要があるかもしれません。

Spring徹底入門 Spring FrameworkによるJavaアプリケーション開発 | 株式会社NTTデータ |本 | 通販 | Amazon

環境インストール

A 付録を参考にSTSをインストールする。

Hello World

13章Spring BootでHello Worldアプリケーションを作成する。

少し複雑なアプリケーション作成

14章のチュートリアルを進める。

ここまできたら実際にWebアプリケーションを作成しながらその他の章を参照するのが良いと思います。

※)英語が苦にならない人は、Guidesも参考になります。

【Spring】Scheduling Tasks

Spring Webアプリで定期実行処理を実現するためには、ScheduledTasksを使う。便利。

Applicationクラスに@EnableSchedulingを追加する

@SpringBootApplication
@EnableScheduling
public class StsTodoApplication {
...
}

コンポーネント作成

@Component
@Slf4j
public class ScheduledTasks {
    
    @Scheduled(fixedRate = 60000)
    public void reportCurrentTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        log.info("The time is now {}", dateFormat.format(new Date()));
    }
}

2019-04-28 17:16:09.671  INFO 6915 --- [   scheduling-1] jp.smaphonia.ststodo.ScheduledTasks      : The time is now 17:16:09

定期実行する間隔はプロパティファイルで設定することも可能。fixedRateではなく、fixedRateStringになっていることに注意。

@Component
@Slf4j
public class ScheduledTasks {
    
    @Scheduled(fixedRateString = "${scheduler.interval}")
    public void reportCurrentTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        log.info("The time is now {}", dateFormat.format(new Date()));
    }
}

application.properties

# スケジューラーインターバル
scheduler.interval=60000

application.propertiesに出ているワーニングを解決すると、 src/main/java/META-INFOにadditional-spring-configuration-metadata.jsonが作成される。

{"properties": [{
  "name": "scheduler.interval",
  "type": "java.lang.String",
  "description": "スケジューラーインターバル"
}]}

リンク

Spring Securityでパスワードエンコーディング未実施のパスワードを使う

ちょっとしたテストでインメモリユーザーを使う場合にエラーが発生する。

java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"

パスワードの前に{noop}を追加すればOK。

変更後

    @Autowired
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
            .withUser("user").password("{noop}password").roles("USER");
    }

変更前

    @Autowired
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
            .withUser("user").password("password").roles("USER");
    }

Spring Boot + Doma2 + SQL Serverでプロジェクト作成(メモ)

Spring Boot + Doma2 + SQL Serverでプロジェクトを作成する場合のメモです。

プロジェクト作成

Spring スターター・プロジェクトを選択

  • Web
  • JDBC(Domaがpomの依存関係に追加される)
  • Thymeleaf(テンプレートエンジン)
  • セキュリティ
  • lombok

Microsoft SQL Serverの依存関係を追加

使うJavaのバージョンに合わせる。

     <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>7.2.1.jre8</version>
            <scope>test</scope>
        </dependency>

最終的なpom.xml

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>7.2.1.jre8</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

DataSourceを設定する。

application.properties

spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=testdb
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver

接続に失敗する場合には、TCP/IP接続が有効になっているかどうか確認する。

Configuring Spring Boot for Microsoft SQL Server - Spring Framework Guru