5

I got this DTO:

@Introspected
data class SomeDTO(
        val someLocalDateTime: LocalDateTime,
        val someString: String
)

And I want to use it in a Micronaut Controller like this:

@Post
@Status(HttpStatus.CREATED)
fun somePostCall(
    someDTO: SomeDTO,
    authentication: Authentication
) {
    this.someMethodCall(
            someDTO.someString,
            someDTO.someLocalDateTime,
            authentication.name
    )
}

I'm getting always this error:

Required argument [SomeDTO someDTO] not specified

I already tried to annotate the value in the DTO with @JsonFormat, @Format and with a custom TypeConverter (String to LocalDateTime) but none of them worked.

1 Answers1

4

Try that ;-)

data class SomeDTO(
        @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
        val someLocalDateTime: LocalDateTime,
        val someString: String
)

If you would do it only for one class. Otherwise you could do it also on a global level.

public class Application {

    public static void main(String[] args) {
        Micronaut.run(Application.class);
    }

    @Singleton
    static class ObjectMapperBeanEventListener implements BeanCreatedEventListener<ObjectMapper> {

        @Override
        public ObjectMapper onCreated(BeanCreatedEvent<ObjectMapper> event) {
            final ObjectMapper mapper = event.getBean();
            mapper.registerModule(new JavaTimeModule());
            mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
            mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            return mapper;
        }
    }
}
IEE1394
  • 1,181
  • 13
  • 33
  • Thanks for your reply, I thought that @Body is not necessary, because at another Controller it works without @Body, and if I change the LocalDateTime Type to String, it also works without @Body. However, now I'm getting this error: Failed to convert argument [someDTO] for value [null] due to: Cannot deserialize value of type `java.time.LocalDateTime` from String \"2020-09-14 10:30:00\": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2020-09-14 10:30:00' could not be parsed at index 10\n at [Source: UNKNOWN; line: -1, column: -1] –  Sep 13 '20 at 20:18
  • ah sorry .. didnt know that .. then you have to tune jackson ;-) cause default its expecting another Format: see https://stackoverflow.com/questions/53195071/how-to-configure-jackson-to-use-snake-case-in-micronaut how to do that and https://www.baeldung.com/jackson-serialize-dates – IEE1394 Sep 13 '20 at 21:10
  • So check the following: 1. having the Jackson JSR310 Lib: https://github.com/FasterXML/jackson-modules-java8 2. add the JavaTimeModule to jackson by using the BeanCreatedEventListener 3. Use the correct timestamp format or specify your own – IEE1394 Sep 13 '20 at 21:11
  • 1
    Ok, I got it finally, here's what I did: I removed (accidentally) the @Introspective Annotation and added the @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss") Annotation to the class variable and then it worked. Do you want to edit your answer, so I can check it green? ;) –  Sep 13 '20 at 21:34