¿Cómo aceptar parámetros de fecha en una solicitud GET a Spring MVC Controller?
Tengo una solicitud GET que envía una fecha en formato AAAA-MM-DD a un controlador de resorte. El código del controlador es el siguiente:
@RequestMapping(value="/fetch" , method=RequestMethod.GET)
public @ResponseBody String fetchResult(@RequestParam("from") Date fromDate) {
//Content goes here
}
La solicitud se envía correctamente ya que estoy comprobando con Firebug. Me sale el error:
Estado HTTP 400: La solicitud enviada por el cliente era sintácticamente incorrecta.
¿Cómo puedo hacer que el controlador acepte este formato de Fecha? Por favor, ayuda. ¿Qué estoy haciendo mal?
100
2 answers
Ok, lo resolví. Escribirlo para cualquiera que pueda estar cansado después de un día completo de codificación sin parar y perderse una cosa tan tonta.
@RequestMapping(value="/fetch" , method=RequestMethod.GET)
public @ResponseBody String fetchResult(@RequestParam("from") @DateTimeFormat(pattern="yyyy-MM-dd") Date fromDate) {
//Content goes here
}
Sí, es simple. Simplemente agregue la anotación DateTimeFormat.
200
Author: LittleLebowski,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-01 19:03:20
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-01 19:03:20
Esto es lo que hice para obtener la fecha formateada de front end
@RequestMapping(value = "/{dateString}", method = RequestMethod.GET)
@ResponseBody
public HttpStatus getSomething(@PathVariable @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) String dateString) {
return OK;
}
Puedes usarlo para conseguir lo que quieres.
4
Author: AbdusSalam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-04 03:05:36
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-04 03:05:36