3

I'm trying Micronaut framework and i'm struggling with Form Urlencoded list I've tried:

public record Request(
        String someThing,
        @JsonFormat(shape = JsonFormat.Shape.ARRAY,pattern = "messages\\[.*?\\]")
        String[] messages,

        @JsonProperty("messages0")
        String[] messages0,

        @JsonProperty("messages1")
        String[] messages1,

        @JsonAlias({"messages2"})
        List<String> messages2,

        @JsonProperty("messages3")
        Map<Object, Object> messages3
) {
}

With the Controller:

@Post(produces = MediaType.APPLICATION_JSON, consumes = {MediaType.APPLICATION_FORM_URLENCODED, MediaType.MULTIPART_FORM_DATA})
public HttpResponse<Response> index(@Body Request request) {
    LOGGER.info("Got: {}", request);
    return HttpResponse.ok(
            new Response(false,"test")
    );
}
[...]
private record Response(
        Boolean success,
        String reason,
) {
}

The curl code is:

curl --request POST \
--url http://localhost:9080/test \
  --header 'Content-Type: application/x-www-form-urlencoded' \
  --data 'messages[0]=Hello world ' \
  --data 'messages0[]=test0' \
  --data 'messages1[]=test1' \
  --data 'messages2[0]=test2' \
  --data 'messages3[]=test3' \
  --data 'messages2[]=test2-2'

Is there a way to deserialize messages into my Request object ?

Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
Paul
  • 63
  • 1
  • 6

0 Answers0