gpt4 book ai didi

java - org.springframework.web.bind.MissingServletRequestParameterException : Required Date parameter 'startTime' is not present

转载 作者:行者123 更新时间:2023-12-01 11:58:03 27 4
gpt4 key购买 nike

我有一个 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

如何让 Controller 接受这种日期时间格式?

最佳答案

value="/{startTime}/{endTime}",这里startTimeendTime是你的路径变量,删除 @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

小例子 -

假设这个网址

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

关于java - org.springframework.web.bind.MissingServletRequestParameterException : Required Date parameter 'startTime' is not present,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28275952/

27 4 0