Spring / 스프링 프레임워크 / 기초 / api

권나영·2021년 10월 22일
0

Spring 기초

목록 보기
3/14

ResponseBody로 값 바로 반환해보기

    @GetMapping("hello-return")
    @ResponseBody
    public String helloreturn(@RequestParam("name") String name){
        return "hello" + name;
    }

바로 return 값이 나옴
(물론 html 아니라는거)

Getteer-Setter 만들기

    @GetMapping("hello-return")
    @ResponseBody
    public String helloreturn(@RequestParam("name") String name){
        return "hello" + name;
    }

    @GetMapping("hello-api")
    @ResponseBody
    public Hello
    
    static class Hello {
        private String name
    }

이 상태로 두고

Alt + Insert 누르면 Getter and Setter 나옴

return은 객체로 내보내기

ResponseBody 부분은 따로 또 바꿔주기

json 형식으로 나타남

ResponseBody 사용 원리

  1. 웹브라우저에서 톰캣 서버로 localhost:8080/hello-api?name=spring을 던졌어
  2. 톰캣 서버는 spring에 이걸 던지면 helloController에서 찾아.
  3. @ResponseBody를 찾아서 return을 하려고 보니까 객체야.
  4. 객체면 HttpMessageConverter에서 처리를 해줘.
  5. 이경우, JsonConverter가 {name:spring}으로 보내줬어
profile
나영

0개의 댓글