코딩하는 오징어

Spring 4 mvc에서 Restful API 본문

Framework/Spring

Spring 4 mvc에서 Restful API

코딩하는 오징어 2018. 3. 6. 23:27
반응형

dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- RequestMappingHandlerMapping, RequestMappingHandlerAdapter 클래스를 빈으로 등록해줌 -->
<mvc:annotation-driven />
<!-- This tag allows for mapping the DispatcherServlet to "/" 이거 없으면 컨트롤러가 있어도 404에러 뜸 -->
<mvc:default-servlet-handler />

<!-- Process annotations on registered beans like @Autowired... -->
<context:annotation-config/>
<!-- 컴포넌트 스캔 -->
<context:component-scan base-package="com.codingsquid.springMVCTest.controller" />


</beans>


pom.xml

<!-- json data bind를 위한 module (이것이 없으면 json, xml데이터 바인딩이 안됨) -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>


<!-- spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>


RestTest.java

package com.codingsquid.springMVCTest.dto;

import lombok.Data;

@Data
public class RestTest {

private String name;
private int age;
private String currentTime;

}


RestAPITestController.java

package com.codingsquid.springMVCTest.controller;

import com.codingsquid.springMVCTest.dto.RestTest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
public class RestAPITestController {

@RequestMapping(value = "/test", method = RequestMethod.GET)
public RestTest testRestAPI() {
RestTest test = new RestTest();
test.setName("codingsquid");
test.setAge(25);
test.setCurrentTime(LocalDateTime.now().toString());
return test;
}

}

여기서 @RestController를 집고 넘어가자. @RestController는 스프링4.0부터 나온 것으로 @Controller와 @ResoponseBody를 합쳐 놓은 것이다. @Controller를 이용하여 Restful API를 만들려면 밑에 코드와 같이 @ResponseBody를 붙여야한다.

package com.codingsquid.springMVCTest.controller;

import com.codingsquid.springMVCTest.dto.RestTest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;

@Controller
public class RestAPITestController {

@RequestMapping(value = "/test", method = RequestMethod.GET)
public @ResponseBody RestTest testRestAPI() {
RestTest test = new RestTest();
test.setName("codingsquid");
test.setAge(25);
test.setCurrentTime(LocalDateTime.now().toString());
return test;
}

}

참고 블로그 : http://highcode.tistory.com/24


결과


반응형
Comments