SPRING/접했던 오류
@RequestBody
고민말고생각하는사람
2024. 8. 6. 20:56
클라이언트에서 보낸 JSON 타입의 데이터를 객체와 바인딩해주는 애노테이션이다.
에러 내용
There was an unexpected error (type=Unsupported Media Type, status=415).
Content-Type 'application/x-www-form-urlencoded;charset=UTF-8' is not supported.
org.springframework.web.HttpMediaTypeNotSupportedException: Content-Type 'application/x-www-form-urlencoded;charset=UTF-8' is not supported
at
spring web 에서
content tpye : application/x-www-form-urlencoded;charset=UTF-8 은 지원하지 않는단다.
상황
html 의 form 데이터를 객체에 바인딩 해주는 기술을 찾다 @RequestBody 이후 에러 발생.
@PostMapping("/users/new")
public String create(@RequestBody UserFormDto dto) {
User user = new User(dto.getName());
System.out.println("user = " + user);
userService.join(user);
return "redirect:/";
}
시도
content type과 charset=utf-8 에 꽂혀 HTML 파일의 charset, entype 등을 수정. -> 실패
원인
@RequestBody 는 JSON 을 객체에 바인딩 해주는 기술이다.
JSON 을 바인딩하기 위한 기술로 application/json을 받는다. HTML 의 form 태그의 인코딩 타입 application/x-www-form-urlencoded 을 받아들이지 못해 에러로 이어졌다.
문제 해결
@RequestBody 를 지웠더니 해결됐다. 바인딩이 잘 된다. (??????)
여기서 사용된 form 데이터 바인딩에 관련해선 추후 학습해보겠다.
결론
@RequestBody 는 JSON 받아와 처리해주는 애노테이션이다.
따라서, HTML의 form 타입과 호환되지 않는다.
하고자하는 작업, 자료 타입을 고려하면서 개발하자.