Java Spring启动自定义HTTP异常
我有这个方法来处理所有缺少异常的请求头,但在一个控制器中,预期会收到一个json作为主体。如果它是无效的json或为空,则会删除带有自定义消息的异常:Java Spring启动自定义HTTP异常,java,spring,spring-boot,spring-mvc,Java,Spring,Spring Boot,Spring Mvc,我有这个方法来处理所有缺少异常的请求头,但在一个控制器中,预期会收到一个json作为主体。如果它是无效的json或为空,则会删除带有自定义消息的异常: @ExceptionHandler(value = {ServletRequestBindingException.class, HttpMessageNotReadableException.class}) public final ResponseEntity<ErrorResponse> handleHeaderExceptio
@ExceptionHandler(value = {ServletRequestBindingException.class, HttpMessageNotReadableException.class})
public final ResponseEntity<ErrorResponse> handleHeaderException(Exception ex) {
List<String> details = new ArrayList<>();
details.add(ex.getMessage());
return new ResponseEntity<>(new ErrorResponse("Bad Request", details), HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(值={ServletRequestBindingException.class,HttpMessageTreadableException.class})
公共最终响应处理例外(例外除外){
列表详细信息=新建ArrayList();
details.add(例如getMessage());
返回新的ResponseEntity(新的ErrorResponse(“错误请求”,详细信息),HttpStatus.Bad_请求);
}
{
“消息”:“错误请求”,
“详情”:[
“缺少必需的请求正文:public org.springframework.http.ResponseEntity
packages.fazerLogin(packages.BodyLogin)抛出java.io.IOException“
]}
{ “消息”:“错误请求”, “详情”:[ “JSON分析错误:意外字符(“\”(代码34)):应使用逗号分隔对象项;嵌套异常为” com.fasterxml.jackson.core.JsonParseException:意外字符 (“\”(代码34)):应使用逗号分隔\n处的对象项 [来源:(PushbackInputStream);行:3,列:3] ]} 但是我不想要像上面这样的长消息。只要“Required request body”或“JSON parse error”就可以了。我想知道我能做什么 我的控制器:
@PostMapping(value = "v1/token", consumes = "application/json;charset=UTF-8")
public ResponseEntity<TokenOutputDto> doLogin(@RequestBody @Valid BodyLogin body) throws IOException {
return authenticationModel.auth(body.getEmail(), body.getPassword());
}
@PostMapping(value=“v1/token”,consumes=“application/json;charset=UTF-8”)
公共响应属性doLogin(@RequestBody@Valid BodyLogin body)引发IOException{
返回authenticationModel.auth(body.getEmail(),body.getPassword());
}
另外,我是否应该为每个可能的异常(HttpClientErrorException、HttpServerErrorException等)创建@ExceptionHandler方法?这将是一个糟糕的做法,因为代码将几乎相同地重复…您可以通过以下方式在方法中处理这些异常处理程序:
@ExceptionHandler(value = {ServletRequestBindingException.class, HttpMessageNotReadableException.class})
public final ResponseEntity<ErrorResponse> handleHeaderException(Exception ex) {
List<String> details = new ArrayList<>();
if (ex instanceof IOException ) {
if (ex.getCause() instanceof JsonParseException) {
details.add("JSON parse error");
} else {
details.add("Required request body");
}
} else {
details.add(ex.getMessage());
}
return new ResponseEntity<>(new ErrorResponse("Bad Request", details), HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(值={ServletRequestBindingException.class,HttpMessageTreadableException.class})
公共最终响应处理例外(例外除外){
列表详细信息=新建ArrayList();
如果(例如IOException的实例){
if(例如,getCause()instanceof JsonParseException){
添加(“JSON解析错误”);
}否则{
详细信息。添加(“所需请求机构”);
}
}否则{
details.add(例如getMessage());
}
返回新的ResponseEntity(新的ErrorResponse(“错误请求”,详细信息),HttpStatus.Bad_请求);
}
您可以通过以下方式在方法中处理这些异常处理程序:
@ExceptionHandler(value = {ServletRequestBindingException.class, HttpMessageNotReadableException.class})
public final ResponseEntity<ErrorResponse> handleHeaderException(Exception ex) {
List<String> details = new ArrayList<>();
if (ex instanceof IOException ) {
if (ex.getCause() instanceof JsonParseException) {
details.add("JSON parse error");
} else {
details.add("Required request body");
}
} else {
details.add(ex.getMessage());
}
return new ResponseEntity<>(new ErrorResponse("Bad Request", details), HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(值={ServletRequestBindingException.class,HttpMessageTreadableException.class})
公共最终响应处理例外(例外除外){
列表详细信息=新建ArrayList();
如果(例如IOException的实例){
if(例如,getCause()instanceof JsonParseException){
添加(“JSON解析错误”);
}否则{
详细信息。添加(“所需请求机构”);
}
}否则{
details.add(例如getMessage());
}
返回新的ResponseEntity(新的ErrorResponse(“错误请求”,详细信息),HttpStatus.Bad_请求);
}
否,如果需要以不同的方式处理每个异常,则需要不同的方法否,如果需要以不同的方式处理每个异常,则需要不同的方法如果(ex instanceof IOException)始终为false,因为ex是一个异常。。。对吗?好的,那么你应该检查它的原因,是的,它是如何工作的?异常被某个点包装,然后您可以检查该异常是否被包装为第二级或第三级,直到您发现itif(ex instanceof IOException)将始终为false,因为ex是一个异常。。。对吗?好的,那么你应该检查它的原因,是的,它是如何工作的?异常被某个点包装,然后您可以检查该异常是否被包装为第二级或第三级,直到找到它为止