org.springframework.web.bind.MissingServletRequestParameterException: Required Date 参数 'startTime'



>我有一个 GET 请求,它将 YYYY-MM-DD hh:mm:ss 格式的日期发送到 Rest 控制器。

法典:

 @RequestMapping(value="/{startTime}/{endTime}", method=RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public @ResponseBody Object load(@RequestParam(value="startTime")  @DateTimeFormat(pattern="yyyy-MM-dd hh:mm:ss") Date startTime,@RequestParam(value="endTime") @DateTimeFormat(pattern="yyyy-MM-dd hh:mm:ss") Date endTime) throws Exception {
        logger.info("GET ALL APPOINTMENTS");
        SuccessResponse<List<TnAppointment>> resp = new SuccessResponse<List<TnAppointment>>();
        try
        {
            Collection<TnAppointment> sequenceCollection = appointmentDelegate.load(startTime, endTime);
            List<TnAppointment> appointmentList = new ArrayList<TnAppointment>(sequenceCollection); 
            resp.setList(appointmentList);
            if(resp.getList().isEmpty())
            {
                String localizedErrorMessage = messageSource.getMessage("appointments.nodata.found", null, currentLocale);
                return new EmptySuccessResponse(localizedErrorMessage);
            }
        }
        catch(Exception de)
        {
            de.printStackTrace();
        }
        return resp;
    }

我收到以下错误

  2015-02-02 16:23:59,709 DEBUG [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver] Resolving exception from handler [public java.lang.Object com.**.ui.restcontroller.appointment.AppointmentController.load(java.util.Date,java.util.Date) throws java.lang.Exception]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.springframework.web.bind.annotation.PathVariable @org.springframework.format.annotation.DateTimeFormat java.util.Date for value '2012-06-10 17:00:06'; nested exception is java.lang.IllegalArgumentException: Unable to parse '2012-06-10 17:00:06'
2015-02-02 16:23:59,709 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] Returning cached instance of singleton bean 'myExceptionHandler'

我正在为此方法使用以下网址

http://localhost:**/**/***/appointments/2012-06-10 17:00:06/2012-06-10 17:27:50

如何使控制器接受这种日期时间格式?

value="/{startTime}/{endTime}" ,这里startTimeendTime是你的路径变量,删除@RequestParam并使用@PathVariable

@PathVariable是从 uri 获取一些占位符(春季调用 它是一个 URI 模板) — 参见 Spring 参考章节 16.3.2.2 URI 模板模式

@RequestParam是获取参数 — 参见 Spring 参考章节 16.3.3.3 使用@RequestParam将请求参数绑定到方法参数

小例子——

假设此网址

http://localhost:8080/MyApp/user/1234/invoices?date=12-05-2013(获取 今天的用户 1234 的发票)

这里1234作为路径变量映射到userId

date被映射为request param

@RequestMapping(value="/user/{userId}/invoices", method = RequestMethod.GET)
public List<Invoice> listUsersInvoices(
            @PathVariable("userId") int user,
            @RequestParam(value = "date", required = false) Date dateOrNull) {
  ...
}

还要正确使用模式 - yyyy-MM-dd HH:mm:ss

最新更新