1. 개요


2. 사전조건

Spring CLI가 설치되어 있어야 한다.


3. 프로젝트 생성

$ spring init --dependencies=web,data-rest,thymeleaf spring-boot-app
Using service at https://start.spring.io
Project extracted to '/home/ubuntu/workspace/spring-boot-app'

4. 실행

$ cd spring-boot-app
$ mvn clean install
$ mvn test
$ mvn spring-boot:run
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.1.RELEASE)

2019-11-12 23:07:51.917  INFO 1117 --- [           main] c.example.springbootapp.DemoApplication  : Starting DemoApplication on 192-168-6-133 with PID 1117 (/workspace/spring-boot-app/target/classes started by ubuntu in /home/ubuntu/workspace/spring-boot-app)
2019-11-12 23:07:51.919  INFO 1117 --- [           main] c.example.springbootapp.DemoApplication  : No active profile set, falling back to default profiles: default
2019-11-12 23:07:52.865  INFO 1117 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-11-12 23:07:52.873  INFO 1117 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-11-12 23:07:52.874  INFO 1117 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.27]
2019-11-12 23:07:52.917  INFO 1117 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-11-12 23:07:52.918  INFO 1117 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 929 ms
2019-11-12 23:07:53.401  INFO 1117 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-11-12 23:07:53.536  WARN 1117 --- [           main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2019-11-12 23:07:53.685  INFO 1117 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-11-12 23:07:53.687  INFO 1117 --- [           main] c.example.springbootapp.DemoApplication  : Started DemoApplication in 2.052 seconds (JVM running for 2.308)

5. 코드 추가

$ mkdir -p src/main/java/com/example/springbootapp
$ vi src/main/java/com/example/springbootapp/APIController.java

springbootapp/APIController.java

package com.example.springbootapp;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class APIController {
  @GetMapping("/api")
  public String index() {
    return "Welcome to Spring Boot App";
  }
  @RequestMapping(value = "/api/hello", method = RequestMethod.GET,
    produces = MediaType.APPLICATION_JSON_VALUE)
  @ResponseBody
  public String hello(@RequestParam String name) {
    String responseJson = "{ \"message\" : \"Hello "+ name + "\" }";
    return responseJson;
  }
}
$ mvn clean install
$ mvn spring-boot:run

다른 창을 열어 확인.

$ curl -X GET 'http://localhost:8080/api/hello?name=world'
{ "message" : "Hello world" }

6. GitHub 연결

6.1. GitHub에서 Public 리파지토리 하나 생성

6.2. 연결

git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/<사용자>/spring-boot-app.git
git push -u origin master