Search code examples
javajson-deserializationmicronaut

Micronaut, declarative Http-Client, JSON serialization wraps HTTP Post object


i am using micronaut to write a service communicating with another service.

I am using declarative http-client.

While sending a request object with declarative http-client, while serialization, something wraps the object equal to the parameter name of the

I have an Object which shall be serialized

@Getter // lombok
@Setter
@Serdeable
public class RequestObject {
private Long id;
}

The http-client looks like the following, using documentation. (Jackson is excluded, only Micronaut SerDes is use)

@Client(id = "thirdservice")
@Header(name = ACCEPT, value = "application/vnd.github.v3+json, application/json")
public interface RemotServiceClient extends RemotServiceOperations {

@Post("/api/endpoint")
ObjectResponse createPostRequest(RequestObject requestObject); // this parameter name is used to wrap

So when i expect the client to serialize RequestObject i would assume the serialization looks like:

{ "id": "123"}

instead it is serializing

{ "requestObject" : { "id": "123"}}

I was reading about @JsonRootName which is disabled by default. Also when i set @JsonRootName("differentValue) there is not effect.

I guess it is some kind of settings within the http-client, which i cannot find out for now. Any tips appreciated.

Also i would like to avoid serializing manually the json from the object, like it is described here.


Solution

  • Annotate the method parameter by io.micronaut.http.annotation.Body annotation:

    @Post("/api/endpoint")
    ObjectResponse createPostRequest(
        @Body RequestObject requestObject
    );
    

    Then it will be serialized as you expected:

    { "id": "123"}
    

    Documentation https://docs.micronaut.io/latest/guide/index.html#clientParameters


    The same behavior is when you write a server (controller).

    Where the variable resolution is moreover better explained in the documentation: https://docs.micronaut.io/latest/guide/index.html#_variable_resolution