P
P
P_Alexander2019-10-02 09:30:08
Java
P_Alexander, 2019-10-02 09:30:08

When I send a POST request, I get a Bad request. Problem with converting date to string. How to decide?

There are such fields in the form

<div class="form-group">
                            <label class="labelForm" for="formBirthDay">Birth day</label>
                            <input type="date" class="form-control" th:field="*{birthDate}" id="formBirthDay" placeholder="Birth day">
                        </div>

If you send the form without a date, then everything is fine, but if you specify a date, I get a Bad request, errors do not fall into the console.
I read that this is a problem with converting the date to a string. The date type in the LocalDate bean.
What I did:
Controller:
@RequestMapping(value = "/signUp", method = RequestMethod.POST)
    public void regCustomer(@ModelAttribute Customer customer) {
        logger.debug("customer " +  customer.toString);
    }

Added a class
@Configuration
public class DateTimeConfig {
    @Bean
    public FormattingConversionService  formattingConversionService() {
        DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(false);

        DateTimeFormatterRegistrar dateTimeFormatterRegistrar = new DateTimeFormatterRegistrar();
        dateTimeFormatterRegistrar.setDateFormatter(DateTimeFormatter.ofPattern("dd.MM.yyyy"));
        dateTimeFormatterRegistrar.setDateTimeFormatter(DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss"));
        dateTimeFormatterRegistrar.registerFormatters(conversionService);

        return conversionService;
    }
}

But due to incomplete understanding, this did not solve my question.
What do I need to do in order to resolve this issue!?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
P_Alexander, 2019-10-13
@P_Alexander

I solved the issue by adding an annotation to the @DateTimeFormat(pattern = "yyyy-MM-dd") bean class. I
haven't found another solution yet...(

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question