• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

HttpMediaTypeNotSupportedException 에러 발생

18.10.13 17:04 작성 조회수 2.67k

0

예제에서와 동일하게 JSON을 실어서 POST 요청을 보내는데 응답코드 415 HttpMediaTypeNotSupportedException로 테스트에 실패합니다.

아래는 결과 메시지의 일부입니다.

MockHttpServletRequest:

HTTP Method = POST

Request URI = /users/create

Parameters = {}

Headers = {Content-Type=[application/json;charset=UTF-8], Accept=[application/json;charset=UTF-8]}

Body = {"username":"hello"}

Session Attrs = {}

Handler:

Type = com.coderobin.springmvcsecond.SampleController

Method = public com.coderobin.springmvcsecond.User com.coderobin.springmvcsecond.SampleController.create(com.coderobin.springmvcsecond.User)

Async:

Async started = false

Async result = null

Resolved Exception:

Type = org.springframework.web.HttpMediaTypeNotSupportedException

ControllerTest 코드

@RunWith(SpringRunner.class)

@WebMvcTest(SampleController.class)

public class SampleControllerTest {

@Autowired<p>MockMvc mockMvc;</p><p></p><p>@Test</p><p>public void createUser_JSON() throws Exception {</p><p>    String jsonText = "{\"username\":\"hello\"}";</p><p></p><p>    mockMvc.perform(post("/users/create")</p><p>            .contentType(MediaType.APPLICATION_JSON_UTF8)</p><p>            .accept(MediaType.APPLICATION_JSON_UTF8)</p><p>            .content(jsonText))</p><p>            .andExpect(status().isOk());</p><p>}</p>

Controller 코드

@RestController

public class SampleController {

@PostMapping("/users/create")<p>public User create(@RequestBody User user) {</p><p>    return user;</p><p>}</p>

답변 2

·

답변을 작성해보세요.

0

전체 프로젝트입니다.

https://github.com/inyung-hwang/springboot-mvc

POST뿐 아니라 Controller에서 GET으로 객체를 리턴할 때도 org.springframework.http.converter.HttpMessageNotWritableException 오류가 발생합니다.

객체 <-> JSON 간 변환이 잘 안되는 것 같습니다.

0

415니까 요청에 들어온 미디어 타입을 모르겠다는 에러인데요. @RestController에 @RequestBody를 사용했다면, 스프링 부트가 제공하는 기본 설정으로는 동작해야 정상입니다.

올려주신 코드에서는 별다른 이상한 점은 안보이네요. 혹시 다른 빈 설정을 재정의 하신게 있나요? 전체 플젝을 깃헙에 올려 공유해 주시면 좋겠습니다.