반응형
이 글의 동영상 강의
이번 글은 이전 글에서 만든 프로젝트에 프로그램을 만들어 테스트해봅니다.
Rest API 작성
- MultiProject1 프로젝트에 SampleRest를 생성
- 패키지 : com.example.multi.rest
- 클래스명 : SampleRest
- SampleRest에 다음 코드를 입력합니다.
package com.example.multi.rest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class SampleRest {
@GetMapping("/msg")
public ResponseEntity<String> getMessage(){
return new ResponseEntity<String>("TestOK!", HttpStatus.OK);
}
}
Boot Dashboard 실행
- 메뉴에서 Window-Show View-Other를 선택합니다.
- 검색창에 "boot"를 입력하고, 아래 Boot Dashboard를 더블클릭합니다.
- Boot Dashboard에서 MultiProject1을 클릭하여 선택합니다.
- 상단의 Start Button을 클릭해서 서버를 Start 시킵니다.
- 정상적으로 Start 되면 Console 창에 아래와 같은 로그가 나옵니다.
- Tomcat이 8080 포트로 Start 된 것을 확인할 수 있습니다.
- 브라우저를 열고 SampleRest에서 만든 주소를 입력합니다.
http://localhost:8080/api/msg
- Rest API 테스트가 완료되었습니다.
공통 Class 생성
- MultiCommon 프로젝트에 CommonService class 생성
- 패키지 : com.example.multi.service
- 클래스명 : CommonService
- CommonService에 다음 코드를 입력합니다.
package com.example.multi.service;
public class CommonService {
public String getMessage(String msg) {
return "This is a " + msg;
}
}
공통 Class를 Child 프로젝트에서 사용하기
- 공통 모듈을 참조하기 위해서는 Child 프로젝트에 공통모듈의 dependency를 추가해야 합니다.
- MultiPorject1의 pom.xml에 아래와 같이 dependency를 추가합니다.
- MultiCommon의 pom.xml에 있는 값으 복사해서 넣어주면 됩니다.
<dependency>
<groupId>com.example</groupId>
<artifactId>MultiCommon</artifactId>
</dependency
- MultiCommon의 version이 없다는 오류 메시지가 나오면 version을 추가합니다.
- MultiCommon의 pom.xml에 version을 추가합니다.
<parent>
<groupId>com.example</groupId>
<artifactId>Multi</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>MultiCommon</artifactId>
<name>MultiCommon</name>
<version>0.1</version>
- MultiProject1의 pom.xml에 version을 추가합니다.
<dependency>
<groupId>com.example</groupId>
<artifactId>MultiCommon</artifactId>
<version>0.1</version>
</dependency>
- Maven Update를 실행합니다.
- MultiProject1의 SampleRest에서 MultiCommon의 CommonService 모듈을 호출하는 코드를 작성합니다.
- SampleRest 코드를 아래와 같이 수정합니다.
package com.example.multi.rest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.multi.service.CommonService;
@RestController
@RequestMapping("/api")
public class SampleRest {
@GetMapping("/msg")
public ResponseEntity<String> getMessage(){
CommonService commonService = new CommonService();
String msg = commonService.getMessage("TestOK!");
return new ResponseEntity<String>(msg, HttpStatus.OK);
}
}
- MultiProject1 프로젝트를 클릭하여 선택하고 상단 실행 버튼으로 프로젝트를 실행합니다.
- 웹브라우저에서 동일하게 테스트를 수행합니다.
- CommonService의 모듈이 정상적으로 작동함을 확인할 수 있습니다.
이상으로 3회에 걸친 Maven을 이용한 스프링부트 Multi Module 프로젝트 개발 과정을 마쳤습니다.
감사합니다.
반응형
'스프링부트' 카테고리의 다른 글
Maven을 이용한 스프링부트 Multi Module 프로젝트(2/3)-프로젝트 만들기 (0) | 2021.10.09 |
---|---|
Maven을 이용한 스프링부트 Multi Module 프로젝트(1/3)-프로젝트구조 (0) | 2021.10.09 |
03.Eclipse에 Springboot 환경 설정 (0) | 2021.08.01 |
02.Eclipse(이클립스) 설치 (0) | 2021.08.01 |
01.Java 설치 및 환경설정 (0) | 2021.08.01 |