Change format timestamp exception Spring

拥有回忆 提交于 2019-12-24 09:47:58

问题


I'm using spring with @ResponseStatusto throw an exception like this:

@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Unknown task") 
public class TaskNotFoundException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    private long taskId;

    public TaskNotFoundException(long taskId) {
        this.taskId = taskId;
    }

    public long getTaskId() {
        return taskId;
    }
}

I get this kind of response:

{
   "timestamp": 1467278537988,
   "status": 404,
   "error": "Not Found",
   "exception": "com.TaskNotFoundException",
   "message": "Unknown Task"
}

I'd like to know how to change the timestamp format in order to have ISO 8601 format. thanks for your help


回答1:


You can customize the error response with @ExceptionHandler and @ResponseBody. For example:

@ExceptionHandler(TaskNotFoundException.class)
@ResponseBody
@ResponseStatus(value = HttpStatus.NOT_FOUND)
private Message handleMessage(TaskNotFoundException e) {
    Message message = new Message();
    message.setTimestamp(ZonedDateTime.now().format(DateTimeFormatter.ISO_INSTANT));
    message.setError("Not Found");
    message.setStatus(404);
    message.setException("com.TaskNotFoundException");
    message.setMessage("Unknown Task");
    return message;
}

Message is simple POJO to hold the error response body.

public class Message {

    private long timestamp;
    private String error;
    private int status;
    private String exception;
    private String message;

    // getters and setters

}

For controller based exception handling, you can add extra @ExceptionHandler methods to any controller to specifically handle exceptions:

@Controller
class ExceptionHandlingController {

    @ExceptionHandler(TaskNotFoundException.class)
    @ResponseBody
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    private Message handleMessage(TaskNotFoundException e) {
       ...
    }

}

For global exception handling, you can use @ControllerAdvice:

@ControllerAdvice
class GlobalControllerExceptionHandler {

    @ExceptionHandler(TaskNotFoundException.class)
    @ResponseBody
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    private Message handleMessage(TaskNotFoundException e) {
       ...
    }

}

More details please check https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc




回答2:


You can use @ExceptionHandler for this and build your own response with ISO 8601 date format :

Below I've attached an example:

@Controller
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public class SomeController {
...
  @ExceptionHandler(TaskNotFoundException.class)
  @ResponseStatus(HttpStatus.NOT_FOUND)
  public @ResponseBody
   Map<String,Object> handleTaskNotFoundException(TaskNotFoundException tnfe,
                                           HttpServletRequest request, HttpServletResponse resp) {
     HashMap<String, Object> result = new HashMap<>();
     DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
     df.setTimeZone(TimeZone.getTimeZone("UTC"));
     String formattedDate = df.format(new Date());
     result.put("8601date", formattedDate);
     ...
     return result;
  }
}


来源:https://stackoverflow.com/questions/38119556/change-format-timestamp-exception-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!