추가적인 GET, POST api를 만들어 보며 API 개발에 익숙해지기
두 수를 입력하면, 다음과 같은 결과가 나오는 GET
API를 만들어 보자!
path : /api/v1/calc
쿼리 파라미터 : num1, num2
{
"add": 덧셈결과,
"minus": 뺄셈결과,
"multiply": 곱셈결과
}
package com.group.libraryapp.controller;
import com.group.libraryapp.domain.Calculator;
import com.group.libraryapp.dto.calculator.response.CalculatorResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CalculatorController {
@GetMapping("/api/v1/calc")
public Calculator calculateTwoNumber(CalculatorResponse response) {
int add = response.getNum1() + response.getNum2();
int minus = response.getNum1() - response.getNum2();
int multiply = response.getNum1() * response.getNum2();
return new Calculator(add, minus, multiply);
}
}
package com.group.libraryapp.domain;
public class Calculator {
private int add;
private int minus;
private int multiply;
public Calculator(int add, int minus, int multiply) {
this.add = add;
this.minus = minus;
this.multiply = multiply;
}
public int getAdd() {
return add;
}
public int getMinus() {
return minus;
}
public int getMultiply() {
return multiply;
}
}
package com.group.libraryapp.dto.calculator.response;
public class CalculatorResponse {
private int num1;
private int num2;
public CalculatorResponse(int num1, int num2) {
this.num1 = num1;
this.num2 = num2;
}
public int getNum1() {
return num1;
}
public int getNum2() {
return num2;
}
}
날짜를 입력하면, 몇 요일인지 알려주는 GET
API를 만들어 보자!
path와 쿼리 파라미터는 임의로 만들어도 상관없다.
GET /api/v1/day-of-week?date=2023-01-01
{
"dayOfTheWeek": "SUN"
}
package com.group.libraryapp.controller;
import com.group.libraryapp.domain.Day;
import com.group.libraryapp.dto.day.request.DayOfTheWeekRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDate;
@RestController
public class DayOfTheWeekController {
@GetMapping("/api/v1/day-of-the-week")
public Day getDay(DayOfTheWeekRequest request) {
LocalDate date = LocalDate.parse(request.getDate());
return new Day(date.getDayOfWeek());
}
}
package com.group.libraryapp.dto.day.request;
public class DayOfTheWeekRequest {
private String date;
public DayOfTheWeekRequest(String date) {
this.date = date;
}
public String getDate() {
return date;
}
}
package com.group.libraryapp.domain;
import java.time.DayOfWeek;
public class Day {
private DayOfWeek dayOfTheWeek;
public Day(DayOfWeek dayOfTheWeek) {
this.dayOfTheWeek = dayOfTheWeek;
}
// 왜 getter가 있어야 되지??
public DayOfWeek getDayOfTheWeek() {
return dayOfTheWeek;
}
}
여러 수를 받아 총 합을 반환하는 POST
API를 만들어 보자!
API에서 받는 Body는 다음과 같은 형태이다. (HINT
: 요청을 받는 DTO에서 List
를 갖고 있으면 JSON의 배열을 받을 수 있습니다.)
{
"numbers": [1,2,3,4,5]
}
반환 결과
15
package com.group.libraryapp.controller;
import com.group.libraryapp.dto.calculator.request.CalculatorAddManyRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class CalculatorController {
@PostMapping("/addMany")
public int addManyNumber(@RequestBody CalculatorAddManyRequest request) {
int sum = 0;
List<Integer> numbers = request.getNumbers();
for (int number : numbers) {
sum += number;
}
return sum;
}
}
package com.group.libraryapp.dto.calculator.request;
import java.util.List;
public class CalculatorAddManyRequest {
List<Integer> numbers;
// 왜 기본 생성자를 생성해줘야 하지?
public CalculatorAddManyRequest() {
}
public CalculatorAddManyRequest(List<Integer> numbers) {
this.numbers = numbers;
}
public List<Integer> getNumbers() {
return numbers;
}
}
자바와 스프링 부트로 생애 최초 서버 만들기, 누구나 쉽게 개발부터 배포까지! [서버 개발 올인원 패키지] - 최태현의 두번째 과제