英文:
Spring-boot HttpMediaTypeNotAcceptableException is not getting caught by @ExceptionHandler in same controller
问题
在我的Spring Boot应用程序中,我有一个用于处理POST请求的控制器,它产生和消费JSON请求。我为这个控制器定义了@ExceptionHandler,用于捕获HttpMediaTypeNotAcceptableException(状态码406)异常。
现在,每当我使用错误的ACCEPT标头(例如application/pdf)调用POST请求时,定义的异常不会被调用。
我不想定义ControllerAdvice,因为我想为这个控制器抛出一个特定的错误。
代码:
@RestController
@RequestMapping("/school")
public class SchoolController {
@Autowired
private SchoolService schoolService;
@PostMapping(produces = "application/json", consumes = "application/json")
@ResponseStatus(HttpStatus.CREATED)
public School postData(@Valid @RequestBody School school) {
return schoolService.save(school);
}
@ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
String handleMediaTypeNotAcceptable(
final HttpMediaTypeNotAcceptableException exception,
final NativeWebRequest request) {
return "acceptable MIME type:" + MediaType.APPLICATION_JSON_VALUE;
}
}
Curl请求:
curl --location --request POST 'http://localhost:8080/school' \
--header 'Accept: application/pdf' \
--header 'Content-Type: application/json' \
--data-raw '{
"studentName":"mayank",
"course":1,
"teacher":"e",
"id":1
}'
所以每当我调用这个Curl请求时,我会得到默认的错误请求响应,控制台日志显示为:"DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]",但它不会调用ExceptionHandler方法。
为什么它没有调用所需的ExceptionHandler方法?
英文:
In my spring boot application, I have a controller for post request which produces and consumes JSON request and I have defined @ExceptionHandler for this controller with HttpMediaTypeNotAcceptableException for catching HttpMediaTypeNotAcceptableException(406 status code) exceptions.
Now whenever I call the post request with a wrong header of ACCEPT such as application/pdf, the defined exception does not get called.
I don't want to define ControllerAdvice since I would like to throw a specific error for this controller.
code:
@RestController
@RequestMapping("/school")
public class SchoolCotroller {
@Autowired
private SchoolService schoolService;
@PostMapping(produces = "Application/Json", consumes = "Application/Json")
@ResponseStatus(HttpStatus.CREATED)
public School postData(@Valid @RequestBody School school) {
return schoolService.save(school);
}
@ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
String handleMediaTypeNotAcceptable(
final HttpMediaTypeNotAcceptableException exception,
final NativeWebRequest request) {
return "acceptable MIME type:" + MediaType.APPLICATION_JSON_VALUE;
}
}
curl:
curl --location --request POST 'http://localhost:8080/school' \
--header 'Accept: application/pdf' \
--header 'Content-Type: application/json' \
--data-raw '{
"studentName":"mayank",
"course":1,
"teacher":"e",
"id":1
}'
So whenever I call this Curl request I get default bad request response with console log as "DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]" but it does not call the ExceptionHandler method.
Why is it not calling the required ExceptionHandler method?
答案1
得分: 0
以下是翻译好的内容:
也许 Spring Boot 在识别 consumes
和 produces
类型时出现问题,因为您在端点中将其提供为 "Application/Json"
,而不是 application/json
。尝试以下代码:
@RestController
@RequestMapping("/school")
public class SchoolController {
@Autowired
private SchoolService schoolService;
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public School postData(@Valid @RequestBody School school) {
return schoolService.save(school);
}
@ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
String handleMediaTypeNotAcceptable(
final HttpMediaTypeNotAcceptableException exception,
final HttpServletRequest request) {
return "可接受的 MIME 类型:" + MediaType.APPLICATION_JSON_VALUE;
}
}
英文:
Maybe springboot is having trouble recognizing the consumes and produces type as application/json
since you provided it as "Application/Json"
at your endpoint.
Try the following code :
@RestController
@RequestMapping("/school")
public class SchoolCotroller {
@Autowired
private SchoolService schoolService;
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE , consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public School postData(@Valid @RequestBody School school) {
return schoolService.save(school);
}
@ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
String handleMediaTypeNotAcceptable(
final HttpMediaTypeNotAcceptableException exception,
final HttpServletRequest request) {
return "acceptable MIME type:" + MediaType.APPLICATION_JSON_VALUE;
}
}
答案2
得分: 0
我遇到了类似的问题。
在抛出错误之前,我添加了Content-Type头,值为MediaType.APPLICATION_JSON_VALUE,将错误状态设置为HttpStatus.NOT_ACCEPTABLE,这样就可以工作了。
英文:
I faced similar issue.
I added Content-Type header as MediaType.APPLICATION_JSON_VALUE before throwing the error as HttpStatus.NOT_ACCEPTABLE and it's working.
专注分享java语言的经验与见解,让所有开发者获益!
评论