Web API
- REST의 HATEOAS와 자체 표현 구조 등 REST API 아키텍처 스타일을 완벽하게 구현하지 못한 API를 의미
Web API 실습
1) PlusResult DTO 클래스 생성
package kr.or.connect.mvcexam.dto;
public class PlusResult {
private int value1;
private int value2;
private int result;
public int getValue1() {
return value1;
}
public void setValue1(int value1) {
this.value1 = value1;
}
public int getValue2() {
return value2;
}
public void setValue2(int value2) {
this.value2 = value2;
}
public int getResult() {
return result;
}
public void setResult(int result) {
this.result = result;
}
}
2) PlusApiController 클래스 생성
package kr.or.connect.mvcexam.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import kr.or.connect.mvcexam.dto.PlusResult;
@Controller
public class PlusApiController {
@GetMapping("/api/plus")
@ResponseBody
public PlusResult plus(@RequestParam("value1") int value1, @RequestParam("value2")int value2) {
PlusResult plusResult = new PlusResult();
plusResult.setValue1(value1);
plusResult.setValue2(value2);
plusResult.setResult(value1+value2);
return plusResult;
}
}
3) json 변환을 위해 pom.xml 파일 업데이트
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.2</version>
</dependency>