Java 更改格式时间戳异常
我使用spring和Java 更改格式时间戳异常,java,spring,date,exception,Java,Spring,Date,Exception,我使用spring和@ResponseStatus引发如下异常: @ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Unknown task") public class TaskNotFoundException extends RuntimeException { private static final long serialVersionUID = 1L; private long taskId;
@ResponseStatus
引发如下异常:
@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;
}
}
我得到这样的回应:
{
"timestamp": 1467278537988,
"status": 404,
"error": "Not Found",
"exception": "com.TaskNotFoundException",
"message": "Unknown Task"
}
我想知道如何更改时间戳格式以获得ISO 8601格式。感谢您的帮助您可以为此使用@ExceptionHandler,并使用ISO 8601日期格式构建自己的响应: 下面我附上了一个例子:
@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;
}
}
@控制器
@RequestMapping(products=MediaType.APPLICATION\u JSON\u值)
公共类控制器{
...
@ExceptionHandler(TaskNotFoundException.class)
@ResponseStatus(未找到HttpStatus.NOT_)
公共@ResponseBody
映射handleTaskNotFoundException(TaskNotFoundException tnfe,
HttpServletRequest请求,HttpServletResponse响应){
HashMap结果=新建HashMap();
DateFormat df=新的SimpleDataFormat(“yyyy-MM-dd'T'HH:MM:ss.S'Z'”);
df.setTimeZone(TimeZone.getTimeZone(“UTC”));
字符串formattedDate=df.format(new Date());
结果。输入(“8601日期”,格式化日期);
...
返回结果;
}
}
您可以为此使用@ExceptionHandler,并使用ISO 8601日期格式构建自己的响应:
下面我附上了一个例子:
@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;
}
}
@控制器
@RequestMapping(products=MediaType.APPLICATION\u JSON\u值)
公共类控制器{
...
@ExceptionHandler(TaskNotFoundException.class)
@ResponseStatus(未找到HttpStatus.NOT_)
公共@ResponseBody
映射handleTaskNotFoundException(TaskNotFoundException tnfe,
HttpServletRequest请求,HttpServletResponse响应){
HashMap结果=新建HashMap();
DateFormat df=新的SimpleDataFormat(“yyyy-MM-dd'T'HH:MM:ss.S'Z'”);
df.setTimeZone(TimeZone.getTimeZone(“UTC”));
字符串formattedDate=df.format(new Date());
结果。输入(“8601日期”,格式化日期);
...
返回结果;
}
}
您可以使用@ExceptionHandler
和@ResponseBody
自定义错误响应。例如:
@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;
}
消息
是保存错误响应正文的简单POJO
public class Message {
private long timestamp;
private String error;
private int status;
private String exception;
private String message;
// getters and setters
}
对于基于控制器的异常处理,您可以向任何控制器添加额外的@ExceptionHandler
方法来专门处理异常:
@Controller
class ExceptionHandlingController {
@ExceptionHandler(TaskNotFoundException.class)
@ResponseBody
@ResponseStatus(value = HttpStatus.NOT_FOUND)
private Message handleMessage(TaskNotFoundException e) {
...
}
}
对于全局异常处理,您可以使用@ControllerAdvice
:
@ControllerAdvice
class GlobalControllerExceptionHandler {
@ExceptionHandler(TaskNotFoundException.class)
@ResponseBody
@ResponseStatus(value = HttpStatus.NOT_FOUND)
private Message handleMessage(TaskNotFoundException e) {
...
}
}
更多详细信息请检查您可以使用
@ExceptionHandler
和@ResponseBody
自定义错误响应。例如:
@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;
}
消息
是保存错误响应正文的简单POJO
public class Message {
private long timestamp;
private String error;
private int status;
private String exception;
private String message;
// getters and setters
}
对于基于控制器的异常处理,您可以向任何控制器添加额外的@ExceptionHandler
方法来专门处理异常:
@Controller
class ExceptionHandlingController {
@ExceptionHandler(TaskNotFoundException.class)
@ResponseBody
@ResponseStatus(value = HttpStatus.NOT_FOUND)
private Message handleMessage(TaskNotFoundException e) {
...
}
}
对于全局异常处理,您可以使用@ControllerAdvice
:
@ControllerAdvice
class GlobalControllerExceptionHandler {
@ExceptionHandler(TaskNotFoundException.class)
@ResponseBody
@ResponseStatus(value = HttpStatus.NOT_FOUND)
private Message handleMessage(TaskNotFoundException e) {
...
}
}
更多详情请查看我仍然有问题。响应头包含我的
@RequestMapping
的http状态@ResponseStatus(HttpStatus.OK)
。如果我将ResponseStatus
添加到@ExceptionHandler(TaskNotFoundException.class)
,它将显示旧的Spring JSON响应。我不确定您的实际原因是因为@ResponseStatus(HttpStatus.OK)
只有在处理方法正常返回时才会返回OK
。如果处理程序引发异常,则注释不适用。我认为您可以在异常处理程序中尝试ResponseEntity
,并在ResponseEntity
中显式设置http状态。我终于找到了答案。我必须去掉我的@ResponseStatus
中的reason
属性,我仍然有一个问题。响应头包含我的@RequestMapping
的http状态@ResponseStatus(HttpStatus.OK)
。如果我将ResponseStatus
添加到@ExceptionHandler(TaskNotFoundException.class)
,它将显示旧的Spring JSON响应。我不确定您的实际原因是因为@ResponseStatus(HttpStatus.OK)
只有在处理方法正常返回时才会返回OK
。如果处理程序引发异常,则注释不适用。我认为您可以在异常处理程序中尝试ResponseEntity
,并在ResponseEntity
中显式设置http状态。我终于找到了答案。我必须去掉我的@ResponseStatus