[인프런 워밍업 클럽] 과제 2일차

진도표 2일차와 연결됩니다

우리는 GET API와 POST API를 만드는 방법을 배웠습니다. 👍 추가적인 API 들을 만들어 보며 API 개발에 익숙해져 봅시다!

 

Controller

@RestController
public class CalculatorController {    

@GetMapping("/api/v1/calc")
    public CalcRequest calc(@RequestParam int num1, @RequestParam int num2) {
        CalcRequest request = new CalcRequest();
        request.setAdd(num1 + num2);
        request.setMinus(num1 - num2);
        request.setMultiply(num1 * num2);
        return request;
    }
}

DTO


public class CalcRequest {
    private int add;
    private int minus;
    private int multiply;



    public int getAdd() {
        return add;
    }

    public int getMinus() {
        return minus;
    }

    public int getMultiply() {
        return multiply;
    }

    public void setAdd(int add) {
        this.add = add;
    }

    public void setMinus(int minus) {
        this.minus = minus;
    }

    public void setMultiply(int multiply) {
        this.multiply = multiply;
    }
}

결과

image

Controller 

@GetMapping("/api/v1/day-of-the-week")
    public DayResponse getDayofWeek(@RequestParam String date) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate = LocalDate.parse(date, formatter);
        String dayOfWeek = localDate.getDayOfWeek().getDisplayName(
                            TextStyle.SHORT, Locale.US).toUpperCase();

        DayResponse response = new DayResponse();
        response.setDayOfTheWeek(dayOfWeek);
        return response;
    }

 

DTO


public class DayResponse {
    private String dayOfTheWeek;

    public String getDayOfTheWeek() {
        return dayOfTheWeek;
    }

    public void setDayOfTheWeek(String dayOfTheWeek) {
        this.dayOfTheWeek = dayOfTheWeek;
    }
}

결과

image

DTO

public class SumRequst {
    private List<Integer> numbers;

    public List<Integer> getNumbers() {
        return numbers;
    }

    public void setNumbers(List<Integer> numbers) {
        this.numbers = numbers;
    }
}

Controller

    @PostMapping("api/v1/sum")
    public int sumNumbers(@RequestBody SumRequst requst) {
        return requst.getNumbers().stream().mapToInt(Integer::intValue).sum();
    }

결과

{
 "numbers" : [1, 2, 3, 4, 5]
}

댓글을 작성해보세요.

채널톡 아이콘