javajacksonlombokmicronautjson-view

How To Enable JsonView With Micronaut


I've got the latest version of Micronaut as of this question (1.1.0) and see that support for the @JsonView jackson annotation has been added. However, when I add it to my controller and enable it in my application.yml, I do not see the annotation being applied to the response, I still receive the full object. Note: I am using Lombok as well with my POJO's, and I don't know if that's interfering.

Controller:

@Controller("/v1")
public class Controller {

private MongoClient client;

public Controller(MongoClient mongoClient) {
    this.client = mongoClient;
}

@Get("/ids")
@Produces(MediaType.APPLICATION_JSON)
@JsonView(Views.IdOnly.class)
public Single<List<Grain>> getIdsByClientId(@QueryValue(value = "clientId") String clientId) {
    return Flowable.fromPublisher(getCollection().find(Filters.eq("data.clientId", clientId))).toList();
}

private MongoCollection<Grain> getCollection() {
    CodecRegistry grainRegistry = CodecRegistries.fromRegistries(MongoClients.getDefaultCodecRegistry(), CodecRegistries.fromProviders(PojoCodecProvider.builder().automatic(true).build()));
    return client
            .getDatabase("db").withCodecRegistry(grainRegistry)
            .getCollection("col", Data.class);
}

}

Data:

@Data
@NoArgsConstructor
public class Data {

    @JsonSerialize(using = ToStringSerializer.class)
    @JsonView(Views.IdOnly.class)
    private ObjectId id;

    private boolean active = true;

    @Valid
    @NotNull
    private DataMeta dataMeta;

    @Valid
    @NotNull
    private DataContent dataContent;

}

View:

public class Views {

    public static class IdOnly {
    }
}

application.yml

---
micronaut:
  application:
    name: mojave-query-api

---
mongodb:
  uri: "mongodb://${MONGO_USER:user}:${MONGO_PASSWORD:password}@${MONGO_HOST:localhost}:${MONGO_PORT:27017}/db?ssl=false&authSource=db"

---
jackson.json-view.enabled: true

application.yml (alternate version also didn't work)

---
micronaut:
  application:
    name: mojave-query-api

---
mongodb:
  uri: "mongodb://${MONGO_USER:user}:${MONGO_PASSWORD:password}@${MONGO_HOST:localhost}:${MONGO_PORT:27017}/db?ssl=false&authSource=db"

---
jackson:
  json-view:
    enabled: true

I'm not sure if I have the jackson line in the wrong place in the application.yml file or if the feature isn't working as intended, or something totally different that I'm missing? Input appreciated!


Solution

  • Last version application.yml is correct, but you forget to mark your Data class as @JsonView class, so the working version is

    
    @Data
    @JsonView
    @NoArgsConstructor
    public class Data {
    
        @JsonSerialize(using = ToStringSerializer.class)
        @JsonView(Views.IdOnly.class)
        private ObjectId id;
    
        private boolean active = true;
    
        @Valid
        @NotNull
        private DataMeta dataMeta;
    
        @Valid
        @NotNull
        private DataContent dataContent;
    
    }