Programming/JAVA

Maven에서 Spring Boot로 API만들기 - 설정

neopin 2021. 5. 22. 23:29

이미 pom.xml 이 있는 maven에서 api를 구현하기위해 Spring이나 Spring Boot를 사용해야하는 상황.

편의를 위해 Spring Boot로 API를 만드는 방법을 정리합니다.

 

  • 현재 상황 
    • Group Id와 Artifact이 지정된 maven 프로젝트가 있다.
      • Group Id: com.jang.proj
      • Artifact Id: api-service
  • 목표 구조

자 이제 시작합니다.

 

1. pom.xml 설정

1.1. parent 설정

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
		<relativePath />
	</parent>

 

1.2. dependency 설정

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>

 

1.3 기타 설정

1.3.1 자바 1.8 설정

	<properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>

 

2. application.yml 설정

server:
  port: 13434
  

spring:
  profiles:
    active: dev

---
spring:
  profiles: dev
  pid:
    file: dev_api_service.pid
  devtools:
    livereload:
      enabled: true

logging:
  level:
    web: DEBUG

---
spring:
  profiles: prod
  

 

3. Main Class 설정

우리 프로젝트의 메인 클래스는 Application.java 입니다.

package com.jang.proj;

import java.util.Iterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.ApplicationPidFileWriter;

@SpringBootApplication(scanBasePackages="com.jang.proj")
public class Application implements ApplicationRunner {

	final static Logger logger = LoggerFactory.getLogger(Application.class);

	public static void main(String[] args) {
		SpringApplication app = new SpringApplication(Application.class);
		app.addListeners(new ApplicationPidFileWriter());
		app.run(args);
	}

	@Override
	public void run(ApplicationArguments args) throws Exception {
		logger.info("Start REST API server!!!");

		Iterator<String> iter = args.getOptionNames().iterator();
		while (iter.hasNext()) {
			String key = iter.next();
			Object value = args.getOptionValues(key);
			logger.info("{}={}", key, value);
		}
	}
}

 

4. Controller 설정

HomeController가 가장 대표적

package com.jang.proj.home;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

	final static Logger logger = LoggerFactory.getLogger(HomeController.class);

	@RequestMapping(value = "/", method = { RequestMethod.GET, RequestMethod.POST })
	public String home(HttpServletRequest request) throws Exception {
		JSONObject json = new JSONObject();

		json.put("success", true);
		json.put("hello", "world");

		return json.toString(4);
	}
}

 

5. 서버 포트 설정

server:
  port: 13434
  

spring:
  profiles:
    active: dev

---
spring:
  profiles: dev
  pid:
    file: dev_api_service.pid
  devtools:
    livereload:
      enabled: true

logging:
  level:
    web: DEBUG

---
spring:
  profiles: prod
  

 

 

실행

Application.java

 

결과

테스트1

테스트2