RestTemplate - 如何以正确的方式显示 HttpServerErrorException - 异常(RestClientException、HttpStatusCodeException)
RestTemplate - How to display HttpServerErrorException in right way - Exceptions (RestClientException, HttpStatusCodeException)
在客户端,我使用 RestTemplate
的 exchange
方法进行 RestFul
网络服务调用。服务调用位于 try/catch block
。当响应代码为 2xx 时一切正常。
我有一个看起来像这样的 catch 块:
catch (HttpServerErrorException e) {
String msg = String.format(
"Error getting book data for bookId: %s and author: %s",
bookId,
author.name,
e.getStatusCode(),
e.getResponseBodyAsString());
LOGGER.error(msg);
}
我想 throw new HttpServerErrorException()
并显示所有需要的字段以供前端开发人员显示它。
我知道我应该做类似 throw new HttpServerErrorException()
的事情
我不知道该怎么做,感谢任何帮助!
您可以创建一个 HttpException class,其中格式在前端是已知的。当您在前端获得非 200 状态代码时,您可以将响应主体序列化为 json,然后从那里提取相关信息。
public class HttpException {
private int statusCode;
public String message;
public int getStatusCode() {
return statusCode;
}
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
}
我找到了一个可行的解决方案,那就是将错误处理集中在一个地方,您可以为此使用一个 @RestControllerAdvice
注释 class.
这是我添加的:
CustomizedResponseEntityExceptionHandler.java
@ControllerAdvice
@RestController
public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(Exception.class)
protected ResponseEntity<Object> handleAllExceptions(Exception ex, WebRequest request) {
ExceptionResponse exceptionResponse =
new ExceptionResponse(new Date(), ex.getMessage(),
request.getDescription(false));
return new ResponseEntity<>(exceptionResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(NotFoundException.class)
protected ResponseEntity<Object> handleUserNotFoundException(NotFoundException ex, WebRequest request) {
ExceptionResponse exceptionResponse =
new ExceptionResponse(new Date(), ex.getMessage(),
request.getDescription(false));
return new ResponseEntity<>(exceptionResponse, HttpStatus.NOT_FOUND);
}
}
ExceptionResponse.java
public class ExceptionResponse {
private Date timestamp;
private String message;
private String details;
public ExceptionResponse(Date timestamp, String message, String details) {
this.timestamp = timestamp;
this.message = message;
this.details = details;
}
}
NotFoundException.java
@ResponseStatus(HttpStatus.NOT_FOUND)
public class NotFoundException extends RuntimeException {
public NotFoundException(String message) {
super(message);
}
}
在 catch 块中,我实现了两个可行的解决方案。
第一个解决方案: 使用 HttpServerErrorException class
第二种解决方案:使用自定义class
catch (HttpServerErrorException e) {
String msg = String.format(
"Error getting book data for bookId: %s and author: %s",
bookId,
author.name,
e.getStatusCode(),
e.getResponseBodyAsString());
LOGGER.error(msg);
//Second solution: throw new NotFoundException("Book with id: " + bookId + " and author " + author.name + " doesn't exist.");
//First solution: throw new HttpServerErrorException(e.getStatusCode(), msg);
}
在客户端,我使用 RestTemplate
的 exchange
方法进行 RestFul
网络服务调用。服务调用位于 try/catch block
。当响应代码为 2xx 时一切正常。
我有一个看起来像这样的 catch 块:
catch (HttpServerErrorException e) {
String msg = String.format(
"Error getting book data for bookId: %s and author: %s",
bookId,
author.name,
e.getStatusCode(),
e.getResponseBodyAsString());
LOGGER.error(msg);
}
我想 throw new HttpServerErrorException()
并显示所有需要的字段以供前端开发人员显示它。
我知道我应该做类似 throw new HttpServerErrorException()
我不知道该怎么做,感谢任何帮助!
您可以创建一个 HttpException class,其中格式在前端是已知的。当您在前端获得非 200 状态代码时,您可以将响应主体序列化为 json,然后从那里提取相关信息。
public class HttpException {
private int statusCode;
public String message;
public int getStatusCode() {
return statusCode;
}
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
}
我找到了一个可行的解决方案,那就是将错误处理集中在一个地方,您可以为此使用一个 @RestControllerAdvice
注释 class.
这是我添加的:
CustomizedResponseEntityExceptionHandler.java
@ControllerAdvice
@RestController
public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(Exception.class)
protected ResponseEntity<Object> handleAllExceptions(Exception ex, WebRequest request) {
ExceptionResponse exceptionResponse =
new ExceptionResponse(new Date(), ex.getMessage(),
request.getDescription(false));
return new ResponseEntity<>(exceptionResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(NotFoundException.class)
protected ResponseEntity<Object> handleUserNotFoundException(NotFoundException ex, WebRequest request) {
ExceptionResponse exceptionResponse =
new ExceptionResponse(new Date(), ex.getMessage(),
request.getDescription(false));
return new ResponseEntity<>(exceptionResponse, HttpStatus.NOT_FOUND);
}
}
ExceptionResponse.java
public class ExceptionResponse {
private Date timestamp;
private String message;
private String details;
public ExceptionResponse(Date timestamp, String message, String details) {
this.timestamp = timestamp;
this.message = message;
this.details = details;
}
}
NotFoundException.java
@ResponseStatus(HttpStatus.NOT_FOUND)
public class NotFoundException extends RuntimeException {
public NotFoundException(String message) {
super(message);
}
}
在 catch 块中,我实现了两个可行的解决方案。
第一个解决方案: 使用 HttpServerErrorException class
第二种解决方案:使用自定义class
catch (HttpServerErrorException e) {
String msg = String.format(
"Error getting book data for bookId: %s and author: %s",
bookId,
author.name,
e.getStatusCode(),
e.getResponseBodyAsString());
LOGGER.error(msg);
//Second solution: throw new NotFoundException("Book with id: " + bookId + " and author " + author.name + " doesn't exist.");
//First solution: throw new HttpServerErrorException(e.getStatusCode(), msg);
}