springboot restful api 서비스 예제
Spring Boot 1.4.3.RELEASE Spring 4.3.5.RELEASE [transitively] Maven 3.1 JDK 1.8 Eclipse MARS.1
- pom.xml 의존성 설정
<modelVersion>4.0.0</modelVersion>
<groupId>com.jepark.springboot</groupId>
<artifactId>SpringBootRestfulApi</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>SpringBootRestfulApi</name>
<description>SpringBootRestfulApi</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Add typical dependencies for a web application -->
<!-- Adds Tomcat and Spring MVC, along others -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<!-- get, headers add : Accept text/xml than present xml type. -->
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- application.yml 설정
server:
port: 8080
contextPath: /SpringBootRestfulApi
com.jepark.springboot com.jepark.springboot.controller com.jepark.springboot.model com.jepark.springboot.service com.jepark.springboot.util@SpringBootApplication(scanBasePackages={"com.jepark.springboot"}) public class SpringBootRestApiExam1Application
This is what our REST API does:
GET request to /api/user/ returns a list of users GET request to /api/user/1 returns the user with ID 1 POST request to /api/user/ with a user object as JSON creates a new user PUT request to /api/user/3 with a user object as JSON updates the user with ID 3 DELETE request to /api/user/4 deletes the user with ID 4 DELETE request to /api/user/ deletes all the users
[ { "id": 1, "name": "Sam", "age": 30, "salary": 70000 }, { "id": 2, "name": "Tom", "age": 40, "salary": 50000 }, { "id": 3, "name": "Jereme", "age": 45, "salary": 30000 }, { "id": 4, "name": "Silvia", "age": 50, "salary": 40000 } ]
Spring’s RestTemplateHTTP Methods and corresponding RestTemplate methods:
HTTP GET : getForObject, getForEntity HTTP PUT : put(String url, Object request, String…urlVariables) HTTP DELETE : delete HTTP POST : postForLocation(String url, Object request, String… urlVariables), postForObject(String url, Object request, Class responseType, String… uriVariables) HTTP HEAD : headForHeaders(String url, String… urlVariables) HTTP OPTIONS : optionsForAllow(String url, String… urlVariables) HTTP PATCH and others : exchange execute
- RestTemplate getForObject > getUserList() getForObject > getUserInfo() postForLocation > createUser() delete > deleteUser() delete > deleteUserAll()
/beans /autoconfig /env