<aside> 📌 진도표 2일차와 연결됩니다 우리는 GET API와 POST API를 만드는 방법을 배웠습니다. 👍 추가적인 API 들을 만들어 보며 API 개발에 익숙해져 봅시다!
</aside>
[Code]
Controller
@GetMapping("/api/v1/calc")
public CalcResponse calc(CalcRequest request) {
return new CalcResponse(request.getNum1(), request.getNum2());
}
CalcRequest
public class CalcRequest {
private int num1;
private int num2;
public CalcRequest(int num1, int num2) {
this.num1 = num1;
this.num2 = num2;
}
public int getNum1() {
return num1;
}
public int getNum2() {
return num2;
}
}
CalcResponse
public class CalcResponse {
private int add;
private int minus;
private int multiply;
public CalcResponse(int num1, int num2) {
this.add = num1 + num2;
this.minus = num1 - num2;
this.multiply = num1 * num2;
}
public int getAdd() {
return add;
}
public int getMinus() {
return minus;
}
public int getMultiply() {
return multiply;
}
}
[API Test]
[Code]
Controller
@GetMapping("/api/v1/day-of-the-week")
public DateResponse dayOfWeek(@RequestParam("date") String date) {
LocalDate localDate = LocalDate.parse(date);
return new DateResponse(localDate);
}