到这个问题为止,我已经得到了Micronaut的最新版本(1.1.0),并且看到已经添加了对@JsonView jackson注释的支持。但是,当我将它添加到我的控制器中并在我的application.yml中启用它时,我没有看到注释被应用于响应,我仍然收到完整的对象。注意:我也在使用Lombok和我的POJO,我不知道这是不是干扰。
控制器:
@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
@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;
}查看:
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: trueapplication.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文件中将jackson行放在了错误的位置,或者功能没有按预期工作,或者我遗漏了一些完全不同的东西?感谢您的参与!
发布于 2019-04-22 11:06:50
最后一个版本的application.yml是正确的,但是您忘记将您的数据类标记为@JsonView类,所以工作版本是
@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;
}https://stackoverflow.com/questions/55747880
复制相似问题