我有一个像下面这样的控制器
@Controller("myController")
@RequestMapping("api")
public class MyController {
@RequestMapping(method = RequestMethod.GET,value = "/get/info/{id}",headers = "Accept=application/json")
public @ResponseBody
Student getInfo(@PathVariable String info) {
.................
}
@ExceptionHandler(Throwable.class)
@ResponseStatus( HttpStatus.EXPECTATION_Failed)
@ResponseBody
public String handleIOException(Throwable ex) {
ErrorResponse errorResponse = errorHandler.handelErrorResponse(ex);
return errorResponse.toString();
}
}
控制器有一个错误处理机制,在错误处理选项中它总是返回期望失败状态代码417.但我需要根据错误类型设置动态错误Http状态代码,如500,403等.我该怎么做呢?
最佳答案
您需要更改输出值ResponseEntity的类型.在这里回答:
How to respond with HTTP 400 error in a Spring MVC @ResponseBody method returning String?
原文链接:https://www.f2er.com/spring/432571.htmlHow to respond with HTTP 400 error in a Spring MVC @ResponseBody method returning String?