1. 개요
2. 환경
Spring Boot Web Starter
3. 코드
3.1. Sarc.java
package com.example.demo;
public class Sarc {
private String url;
private String scheme;
private String articleMenu;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getScheme() {
return scheme;
}
public void setScheme(String scheme) {
this.scheme = scheme;
}
public String getArticleMenu() {
return articleMenu;
}
public void setArticleMenu(String articleMenu) {
this.articleMenu = articleMenu;
}
}
3.2. SarcController.java
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SarcController {
@GetMapping("/sarc")
public Sarc getUrl() {
Sarc sarc = new Sarc();
sarc.setUrl("sarc.io");
sarc.setScheme("https");
sarc.setArticleMenu("aws");
return sarc;
}
}
4. 호출
http://localhost:8080/sarc
5. RestTemplate
RestTemplate template = new RestTemplate();
| HTTP Method | RestTemplate Method |
| GET | getForObject() |
| POST | postForObject() |
| PUT | put() |
| DELETE | delete() |
| OPTIONS | optionsForAllow() |
| PATCH | patchForObject() |
| HEAD | headForHeaders() |
RestTemplate template = new RestTemplate();
String uri = "/store/orders/{id}/items";
OrderItem[] items = template.getForObject(uri, OrderItem[].class, "1");
OrderItem item = //Create Item
URI itemLocation = template.postForLocation(uri, item, "1");
item.setAmount(2); template.put(itemLocation, item);
template.delete(itemLocation);
6. Request Mapping 기반 메소드
- @GetMapping
- @PostMapping
- @PutMapping
- @PatchMapping
- @DeleteMapping
다음은 모든 주문을 가져오는 GET 요청이다.
@GetMapping(path="/orders")
다음은 새로운 주문을 생성하는 POST 요청이다.
@PostMapping(path="/orders")
7. RequestMapping 어노테이션
@RequestMapping(path="/members", method=RequestMethod.GET)
이는 다음과 동일하다.
@GetMapping("/members")
8. RestController 어노테이션
@Controller
public class OrderController {
@GetMapping("/orders/{id}")
public Order getOrder(@PathVariable long id) {
return orderService.findOrder(id);
}
}
이는 다음과 동일하다.
@RestController
public class OrderController {
@GetMapping("/orders/{id}")
public Order getOrder(@PathVariable long id) {
return orderService.findOrder(id);
}
}