[Spring Boot][java] getting started

やっときました。Spring Boot。
まず、Spring Initializrを使います。
dependenciesに「Web」と入力します。

demo.zipをdownloadします。
STSでfile->import->maven->existing maven projects

DemoApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

Run As -> Java ApplicationでTomcatが起動する

2021-01-28 22:00:38.906 INFO 6756 — [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-01-28 22:00:38.906 INFO 6756 — [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1111 ms
2021-01-28 22:00:39.057 INFO 6756 — [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService ‘applicationTaskExecutor’
2021-01-28 22:00:39.219 INFO 6756 — [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ”

controllerの機能を実装する

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {

	@RequestMapping("/")
	String hello() {
		return "Hello World";
	}
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

Web server failed to start. Port 8080 was already in use.

$ lsof -i:8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 6756 mac 41u IPv6 * 0t0 TCP *:http-alt (LISTEN)

src/main/resources/application.properties

server.port=8090

やべ、ちょっと感動した。
– MVC自動設定、Tomcat自動設定、CharacterEncodingFilter自動設定、Logback自動設定、Bean Validation自動設定