user812612
user812612

Reputation: 399

Spring MVC controller with date

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

Upvotes: 9

Views: 18790

Answers (3)

Paul T
Paul T

Reputation: 405

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

Upvotes: 0

Noah Hall-Potvin
Noah Hall-Potvin

Reputation: 187

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

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

Hope this helps!

Upvotes: 11

dimitrisli
dimitrisli

Reputation: 21381

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.

Upvotes: 23

Related Questions