본문 바로가기

Java/오류

[오류] HttpMessageNotReadableException : Required request body is missing

 

DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public org.springframework.http.ResponseEntity<java.lang.String> ... 경로...Controller.deleteEntity(java.util.Map<java.lang.String, java.lang.Object>)]

 

프론트 쪽에서 아래의 함수로 서버에 요청을 보내고

export const deleteEntityApi = async (param) => {
	const response = await api.delete(`ent/${param}`);
  	return response.data;
}

** 요청url 백틱 주의 **

 

서버에서는 아래처럼 받았었는데 Required request body is missing 오류가 떴다.

@DeleteMapping
@ResponseBody
public ResponseEntity<String> deleteEntity(@RequestBody String param){
    //함수로직
}

 

DELETE 는 요청 본문(body)를 포함하지 않아야 하는 HTTP 메소드 중 하나란다!! ( tomcat 파싱 문제..?)

지금까지는 항상 @PostMapping을 사용해왔어서 별생각 없이 @RequestBody를 사용한게 문제였다.

 ' Required request body is missing' 이라는게 말그대로 body에 담긴 데이터를 읽어오지 못한거였다.

 

아래처럼 수정해줬다.

@DeleteMapping(value = "/{entityId}")
@ResponseBody
public ResponseEntity<String> deleteEntity(@PathVariable String param){
    // 함수로직
}

 

@PathVariable / @RequestParam 사용

@PathVariable 는 값을 하나만 받아올 수 있고, @ RequestParam 은 query string(url)이나 body의 값도 받아올 수 있다.

 


이 때 @DeleteMapping(value = "${entityId}") 오타때문에 다시 다른 오류 ..


Caused by: java.lang.RuntimeException: Could not postProcess org.springframework.security.config.annotation.web.builders.WebSecurity@3b3e4fad of type class org.springframework.security.config.annotation.web.builders.WebSecurity

이건 또 왜났는지 모르겠다..


최종적으로는 이렇게 해줬다.

@DeleteMapping("/{param}")
@ResponseBody
public ResponseEntity<String> deleteEntity(@PathVariable String param){
    // 함수로직
}