org.springframework.web.bind.MissingServletRequestParameterException:必需的日期参数' startTime'不在场

时间:2015-02-02 10:45:35

标签: java spring date

我有一个GET请求,它将YYYY-MM-DD hh:mm:ss格式的日期发送给Rest Controller。

代码:

 @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

如何让控制器接受这种DateTime格式?

1 个答案:

答案 0 :(得分:1)

value="/{startTime}/{endTime}",此处startTimeendTime是您的路径变量,请移除@RequestParam并使用@PathVariable

  

@PathVariable是从uri获取一些占位符(Spring调用   它是一个URI模板) - see Spring Reference Chapter 16.3.2.2 URI Template Patterns

     

@RequestParam是获取参数 - see Spring Reference Chapter 16.3.3.3 Binding request parameters to method parameters with @RequestParam

小例子 -

假设此Url

  

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