Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更改格式时间戳异常_Java_Spring_Date_Exception - Fatal编程技术网

Java 更改格式时间戳异常

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;

我使用spring和
@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