How to wrap properties with Jackson (Micronaut)?

Viewed 114

I'm creating a microservice on Micronaut using Jackson. However, I didn't find how it is possible to nest some properties inside the json without creating a separate class?

Entity JPA Class looks like this:

@Entity
@Table(name = "citizens", schema = "main")
@Introspected
public class Citizen {

    @Id
    @Column(name = "oid", nullable = false)
    @JsonView(JsonViewSimple.class)
    private String id;

    @JsonInclude
    @Column(name = "first_name")
    @JsonSerialize(using = WhiteSpaceRemovalSerializer.class)
    @JsonView(JsonViewSimple.class)
    private String firstName;

    @JsonInclude
    @Column(name = "last_name")
    @JsonSerialize(using = WhiteSpaceRemovalSerializer.class)
    @JsonView(JsonViewSimple.class)
    private String lastName;
..

JSON result:


    {
     "id": "123",
     "first_name": "John",
     "last_name": "Wick"
    }

As a result, I want to get first_name and last_name grouped into a json object name


    {
     "id": "123",
     "name: {
        "first_name": "John",
        "last_name": "Wick"
     }
    }

What is the easiest way to do this without resorting to creating a separate child class and many lines of code?

1 Answers

What you are looking for is the opposite of the @JsonUnwrapped annotation, sadly Jackson does not provide this feature at the moment.

There is an issue/feature request on Github where you can add your 'thumbs up' to vote for the feature to be implemented: https://github.com/FasterXML/jackson-annotations/issues/42

Related