Problem with binding in Spring MVC

0

I have the following field in the form

<div class="form-group col-md-2">
                <form:label path="horarioInicio">Horario inicio</form:label>
                <form:input id="horarioInicio" path="horarioInicio"
                    class="form-control input-sm" type="time" />
            </div>

In the template class I have the StartTime attribute

@DateTimeFormat(pattern = "HH:mm")
@Temporal(TemporalType.TIME)
private Date horarioInicio;

The problem occurs when I try to submit the form. I always get the following message:

Field error in object 'ordemServico' on field 'horarioInicio': rejected value [00:00]; codes [typeMismatch.ordemServico.horarioInicio,typeMismatch.horarioInicio,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [ordemServico.horarioInicio,horarioInicio]; arguments []; default message [horarioInicio]]; default message [Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property 'horarioInicio'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat @javax.persistence.Temporal java.util.Date] for value '00:00'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [00:00]

Has anyone ever had a similar problem?

    
asked by anonymous 01.10.2017 / 20:37

1 answer

0

I'm not exactly sure how @DateTimeFormat works. But it probably does not interfere with the creation of the object by spring json parser.

Your problem probably occurs because you are passing a string HH: mm to the Date constructor, which, as far as I know, is not ready to receive this.

According to the documentation, the Date constructor that receives string uses the parse (java.lang.String) of the Date class. You can test this constructor manually with a string "HH: mm" but I believe that is the problem

link

    
02.10.2017 / 21:14