# HttpMessageConverters

```java
@RestController
public class UserController {

    @PostMapping("/user")
    public @ResponseBody User create(@RequestBody User user) {
        return user;
    }
}
```

```
{“username”:”keesun”, “password”:”123”} <-> User
```

* 스프링 프레임워크에서 제공하는 인터페이스
* http 요청 본문을 객체로, 객체를 http 응답 본문으로 변환할 때 사용한다.
* @RequestBody, @ResponseBody와 사용된다.
* @ResponseBody는 @RestController가 붙어있으면 생략할 수 있다.
  * 없는데 달아주지 않으면 뷰를 찾으려고 시도한다.

```java
@WebMvcTest(UserController.class)
public class UserControllerTest {

    @Test
    void createUser_JSON() throws Exception {
        String userJson = "{\"username\":\"keesun\",\"password\":1234}";
        
        mockMvc.perform(post("/users/create")
                        .contentType(MediaType.APPLICATION_JSON)
                        .accept(MediaType.APPLICATION_JSON)
                        .content(userJson))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.username", is(equalTo("keesun"))))
                .andExpect(jsonPath("$.password", is(equalTo("1234"))));

    }
}
```

* 요청할 때 json으로 들어온다는 것을 확인할 수 있다.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dodeon.gitbook.io/study/keesunbaik-concepts-of-spring-boot/05-spring-web-mvc/http-message-converters.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
