Spring :: API 동작 방식

숑숑·2021년 6월 29일
0

Spring

목록 보기
5/11
post-thumbnail

API 예제

    @GetMapping("hello-api")
    @ResponseBody
    public Hello helloApi(@RequestParam("name") String name) {
        Hello hello = new Hello();
        hello.setName(name);
        return hello;
    }

    static class Hello {
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
  • 일반적인 MVC의 컨트롤러와 달리 객체를 반환하는게 특징이다.

  • Spring 에서 객체를 반환할 때는 json 형태로 변환되는게 디폴트다.

  • @Responsebody 를 사용하면 ViewResolver 를 사용하지 않고, HTTP Body에 문자 내용을 직접 삽입한다.


@ResponseBody 원리

  • 객체의 경우 JsonConverter로,
  • static string의 경우 StringConverter가 사용된다.

약간의 커스텀을 통해 XMLConverter 등등 다른 변환기로 디테일하게 설정할 수 있지만 거의 쓰지 않는다고 함.

profile
툴 만들기 좋아하는 삽질 전문(...) 주니어 백엔드 개발자입니다.

0개의 댓글