사용 환경
- Windows 10
- Java 1.8
- Maven 3.8.2
- Spring 5.0.6
Hello 프로젝트 따라하기
-
프로젝트 생성
브라우져에서 spring initializr를 방문하여 아래와 같이 입력하고 GENERATE 버튼을 클릭합니다.
- Project: Maven Project
- Language: Java
- Spring Boot: 2.5.4
- Project Meta:
- Group: trvoid
- Artifact: rest
- Name: rest
- Package name: trvoid
- Packaging: Jar
- Java: 8
- Dependencies: Spring Web
생성된 프로젝트 파일을 다운로드하여 압축을 풉니다. 프로젝트 폴더 구조는 아래와 같습니다.
hello |-src |-main |-java |-trvoid |-RestApplication.java |-test |-java |-trvoid |-RestApplicationTests.java |-pom.xml
pom.xml 파일에서 의존성 항목과 빌드 플러그인을 확인할 수 있습니다.
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
RestApplication.java 파일의 내용은 아래와 같습니다.
package trvoid; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class RestApplication { public static void main(String[] args) { SpringApplication.run(RestApplication.class, args); } }
-
컨트롤러 추가
src/main/java/trvoid/Greeting.java
package trvoid; public class Greeting { private String to; private String message; public Greeting(String to, String message) { this.to = to; this.message = message; } // getters and setters }
src/main/java/trvoid/HelloController.java
package trvoid; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping(value="/{name}") public Greeting sayHello(@PathVariable String name) { return new Greeting("Server", name, "Hello"); } }
-
컴파일
>mvn compile
-
실행
>mvn exec:java -Dexec.mainClass=trvoid.RestApplication
실행 결과
... . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.4) 2021-09-08 10:45:34.564 INFO 39908 --- [lication.main()] trvoid.RestApplication : Starting RestApplication using Java 1.8.0_261 on DESKTOP-NBBJSSF with PID 39908 (D:\DevTest\Spring\rest-maven\target\classes started by WISECAN in D:\DevTest\Spring\rest-maven) 2021-09-08 10:45:34.564 INFO 39908 --- [lication.main()] trvoid.RestApplication : No active profile set, falling back to default profiles: default 2021-09-08 10:45:35.142 INFO 39908 --- [lication.main()] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2021-09-08 10:45:35.157 INFO 39908 --- [lication.main()] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2021-09-08 10:45:35.157 INFO 39908 --- [lication.main()] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.52] 2021-09-08 10:45:35.204 INFO 39908 --- [lication.main()] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2021-09-08 10:45:35.204 INFO 39908 --- [lication.main()] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 617 ms 2021-09-08 10:45:35.563 INFO 39908 --- [lication.main()] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2021-09-08 10:45:35.614 INFO 39908 --- [lication.main()] trvoid.RestApplication : Started RestApplication in 1.292 seconds (JVM running for 16.457)
-
브라우져로 REST API 호출 결과 확인
브라우져에서 주소창에
http://localhost:8080/홍길동
을 입력하고 엔터를 치면 아래와 같은 결과를 얻습니다.{"to":"홍길동","message":"Hello from Server"}
Written with StackEdit.
댓글 없음:
댓글 쓰기