0

I'm getting an error while passing the parameter 'date' to my java api, the code is the following for the controller:

@RequestMapping(value = "/horario/{fecha}", method = RequestMethod.GET)
@ResponseBody
public Object queryHorariosLibres(@PathVariable("fecha") Date fecha) {
    List<Long> horariosLibres = null;
    List<Long> turnosTomados = turnoService.getTurnosTomados(fecha);
    Calendar dia = new GregorianCalendar();
    dia.setTime(fecha);
    Horario horario = horarioRepository.findByDia(dia.get(Calendar.DAY_OF_WEEK));  
    horariosLibres = horario.getHorariosLibres(turnosTomados);
    if (horariosLibres == null) {
        return "hola";
    } else
    return horariosLibres;
}   

And this is the error I'm getting:

There was an unexpected error (type=Bad Request, status=400).
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 java.util.Date for value '2016-02-15'; nested exception is java.lang.IllegalArgumentException

Here's the code for my model: This is the "horario" class which manages hours :

@Entity
@Table(name = "horario")
public class Horario implements Serializable {

private static final long serialVersionUID = 1L;
private Long id;
private Integer dia;
private Long horarioInicio;
private Long horarioFin;

Getters And Setter's

@Transient
public List<Long> getHorarios(){
    List<Long> horarios = new ArrayList<Long>();

    for(Long i = horarioInicio; i <= horarioFin; i+=300){
        horarios.add(i);
    }
    return horarios;

}

@Transient
public List<Long> getHorariosLibres(List<Long> horariosTomados){

    List<Long> horariosLibres = getHorarios();
    horariosLibres.removeAll(horariosTomados);

    return horariosLibres;

}

...

And this is the "turn / schedule" class that manages the appointments:

@Entity
@Table(name = "turno")
public class Turno implements Serializable {

private static final long serialVersionUID = 1L;
private Long id;
private String solicitante;
private String telefono;
private TipoDocumento tipoDocumento;
private String numeroDocumento;
private String email;
private Integer horario;
private String numeroTurno;
private Date fecha;
private String controlFecha;

Getters and Setters
}
2
  • I would Really Really recommend to use joda-time's LocalDate instead of java.util.Date Maybe it will also deserialize out of box. Commented Feb 17, 2016 at 15:42
  • I'll try it, thanks for the advice. Commented Feb 17, 2016 at 18:51

2 Answers 2

1

You need to add the @DateTimeFormat annotation to your parameter.

@DateTimeFormat(pattern="yyyy-MM-dd")

@RequestMapping(value = "/horario/{fecha}", method = RequestMethod.GET)
@ResponseBody
public Object queryHorariosLibres(@PathVariable("fecha") @DateTimeFormat(pattern="yyyy-MM-dd") Date fecha) {
List<Long> horariosLibres = null;
List<Long> turnosTomados = turnoService.getTurnosTomados(fecha);
Calendar dia = new GregorianCalendar();
dia.setTime(fecha);
Horario horario = horarioRepository.findByDia(dia.get(Calendar.DAY_OF_WEEK));  
horariosLibres = horario.getHorariosLibres(turnosTomados);
if (horariosLibres == null) {
    return "hola";
} else
return horariosLibres;
}   
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly that. Forgot completely to format the date. Thanks!
0

The error is clear

Failed to convert from type java.lang.String to type @org.springframework.web.bind.annotation.PathVariable java.util.Date for value '2016-02-15'; nested exception is java.lang.IllegalArgumentException

2016-02-15 cannot be converted to Date class, so it's treated as a String and cannot fit the signature. You should change that field in a way that can be automatically parsed by the controller

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.