9

I've been trying to use:

@RequestMapping(value="/consultaporusuarioperiodo/{idusuario}/{datainicio}/{datafim}", method = RequestMethod.GET)
public String consultaPorPeriodoUsuario(
        @PathVariable("idusuario") Long idUsuario,
        @PathVariable("datainicio") Date dataInicio,
        @PathVariable("datafim") Date dataFim
        ,Model model) {
    Usuario usuario = usuarioService.buscaPorId(idUsuario);
    model.addAttribute("timesheet",timeSheetService.buscaPorPeriodoPorUsuario(dataInicio, dataFim,usuario));
    return "timesheetcrud/consultatimesheet";
}

with this link:

http://localhost:8080/timesheet/consultaporusuarioperiodo/1/21012000/22012000

but I get this error:

HTTP Status 400 -

type Status report

message

description The request sent by the client was syntactically incorrect ().

Apache Tomcat/7.0.27

when I change to:

        @PathVariable("datainicio") String dataInicio,
        @PathVariable("datafim") String dataFim

It's work. What can I do to work with Date ?

thanks

4
  • Try annotating your date parameters with @DateTimeFormat(pattern = "ddMMyyyy") Commented May 30, 2013 at 22:27
  • I've already tried with @DateTimeFormat but when I put @PathVariable("datainicio") @DateTimeFormat(pattern = "ddMMyyyy") Date dataInicio, It's not work too :-( Only work with String. Commented May 31, 2013 at 11:56
  • Tip: Try to use best practices and pass this two date values in Request Body or Query Parameters. apigee.com/about/api-best-practices =) Commented May 31, 2013 at 12:43
  • Mark the answer as correct to help the community :) Also its duplicate of stackoverflow.com/questions/15164864/… Commented Jun 17, 2014 at 11:43

3 Answers 3

23

Try:

    @PathVariable("datainicio") @DateTimeFormat(iso=ISO.DATE) String dataInicio,
    @PathVariable("datafim") @DateTimeFormat(iso=ISO.DATE) String dataFim

where ISO.DATE is of yyyy-mm-dd pattern.

Sign up to request clarification or add additional context in comments.

Comments

11

I had to do something very similar. This is what I did:

@PathVariable("datainicio") @DateTimeFormat(pattern = "ddMMyyyy") Date dataInicio

Hope this helps!

Comments

0

In Spring's spirit, the best choice should be a convention to impose a unified way of handling Date parameters. And luckily it gives you exactly this little cookie - drop it in application.properties and ... Bob's your uncle:

spring.mvc.date-format=ddMMyyyy

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.