Pra*_*dra 9 java spring json spring-mvc spring-boot
我有一个请求映射 -
@RequestMapping("/fetchErrorMessages")
public @ResponseBody int fetchErrorMessages(@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime) throws Exception
{
if(SanityChecker.checkDateSanity(startTime)&&SanityChecker.checkDateSanity(endTime))
{
return 0;
}
else
{
throw new NotFoundException("Datetime is invalid");
}
}
Run Code Online (Sandbox Code Playgroud)
如果startTime和endTime无效,我想抛出500错误但返回JSON中的异常字符串.但是,我得到一个HTML页面而不是说
白标错误页面
此应用程序没有/ error的显式映射,因此您将此视为回退.
Wed Dec 20 10:49:37 IST 2017
出现意外错误(type = Internal Server Error,status = 500).
日期时间无效
我反而希望用JSON返回500
{"error":"Date time format is invalid"}
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
假设您有一个自定义的Exception类NotFoundException及其实现,如下所示:
public class NotFoundException extends Exception {
private int errorCode;
private String errorMessage;
public NotFoundException(Throwable throwable) {
super(throwable);
}
public NotFoundException(String msg, Throwable throwable) {
super(msg, throwable);
}
public NotFoundException(String msg) {
super(msg);
}
public NotFoundException(String message, int errorCode) {
super();
this.errorCode = errorCode;
this.errorMessage = message;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public int getErrorCode() {
return errorCode;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public String getErrorMessage() {
return errorMessage;
}
@Override
public String toString() {
return this.errorCode + " : " + this.getErrorMessage();
}
}
Run Code Online (Sandbox Code Playgroud)
现在您想从控制器抛出一些异常。如果抛出异常,则必须从标准错误处理程序类中捕获该异常,例如在春季说,它们提供了@ControllerAdvice注释以应用于制作标准错误处理程序类。当将它应用于类时,该spring组件(我是说您注释的类)可以捕获从控制器抛出的任何异常。但是我们需要使用适当的方法来映射异常类。因此,我们为您的异常NotFoundException处理程序定义了一种方法,如下所示。
@ControllerAdvice
public class RestErrorHandler {
@ExceptionHandler(NotFoundException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public Object processValidationError(NotFoundException ex) {
String result = ex.getErrorMessage();
System.out.println("###########"+result);
return ex;
}
}
Run Code Online (Sandbox Code Playgroud)
您想将http状态发送到内部服务器error(500),因此在这里我们使用@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)。由于您使用了Spring-boot,因此您无需制作json字符串,只需简单的注释@ResponseBody即可自动完成。
创建自定义异常。
public class SecurityException extends RuntimeException {
private static final long serialVersionUID = -7806029002430564887L;
private String message;
public SecurityException() {
}
public SecurityException(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Run Code Online (Sandbox Code Playgroud)
创建自定义响应实体。
public class SecurityResponse {
private String error;
public SecurityResponse() {
}
public SecurityResponse(String error) {
this.error = error;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
}
Run Code Online (Sandbox Code Playgroud)
为自定义异常创建一个带有 ExceptionHandler 的 ControllerAdvice,它将处理自定义异常,填充并返回自定义响应,如下所示。
@ControllerAdvice
public class SecurityControllerAdvice {
@ExceptionHandler(SecurityException.class)
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public SecurityResponse handleSecurityException(SecurityException se) {
SecurityResponse response = new SecurityResponse(se.getMessage());
return response;
}
}
Run Code Online (Sandbox Code Playgroud)
根据您的条件抛出自定义异常。
throw new SecurityException("Date time format is invalid");
Run Code Online (Sandbox Code Playgroud)
现在运行并测试您的应用程序。EG :
| 归档时间: |
|
| 查看次数: |
20773 次 |
| 最近记录: |