org.springframework.web.bind.MissingServletRequestParameterException:必需的日期参数 'startTime' 不存在
org.springframework.web.bind.MissingServletRequestParameterException: Required Date parameter 'startTime' is not present
我有一个 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'
我正在使用以下 url 此方法
http://localhost:**/**/***/appointments/2012-06-10 17:00:06/2012-06-10 17:27:50
如何让控制器接受这种格式的 DateTime?
value="/{startTime}/{endTime}"
,这里startTime
和endTime
是你的路径变量,去掉@RequestParam
改用@PathVariable
.
@PathVariable is to obtain some placeholder from the uri (Spring call
it an URI Template) — see Spring Reference Chapter 16.3.2.2 URI
Template Patterns
@RequestParam is to obtain an parameter — 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 (to get
the invoices for user 1234 for today)
//这里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
我有一个 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'
我正在使用以下 url 此方法
http://localhost:**/**/***/appointments/2012-06-10 17:00:06/2012-06-10 17:27:50
如何让控制器接受这种格式的 DateTime?
value="/{startTime}/{endTime}"
,这里startTime
和endTime
是你的路径变量,去掉@RequestParam
改用@PathVariable
.
@PathVariable is to obtain some placeholder from the uri (Spring call it an URI Template) — see Spring Reference Chapter 16.3.2.2 URI Template Patterns
@RequestParam is to obtain an parameter — 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 (to get the invoices for user 1234 for today)
//这里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