在来自 Micronaut 休息端点的 JSON 中包含 null 和空属性

问题描述

Micronuat JSON 序列化的默认配置是 JsonInclude.Include.NON_EMPTY,它不包含空值或 null 值。但是,我需要包含空值和空值,所以我到目前为止所做的 -

@Introspected
@JsonInclude(ALWAYS)
public class FindVendorViewModel extends PaginationViewModel {
    private List<VendorViewModel> vendors;

    public List<VendorViewModel> getVendors() {
        return vendors;
    }

    public void setVendors(List<VendorViewModel> vendors) {
        this.vendors = vendors;
    }
}

分页模型

@Introspected
@JsonInclude(ALWAYS)
public class PaginationViewModel {
    int pageSize = 10;
    int total = 0;
    int currentPage = 0;
    int totalPage = 0;
    @Valid
    @LastIdRequired
    String lastId;
   
   // Getter and setter
}

当我添加 @JsonInclude(ALWAYS) 时,它不会在 JSON 响应中包含 NULLEMPTY 值,如下所示。

{
    "pageSize": 10,"total": 28,"currentPage": 0,"totalPage": 3
}

如果我添加以下代码,它会包含 lastIdvendors

NULLEMPTY
jackson:
  serializationInclusion: ALWAYS

{
    "pageSize": 10,"total": 0,"totalPage": 0,"lastId": null,"vendors": []
}

然而,它抛出了如下异常

11:15:25.640 [default-nioEventLoopGroup-1-5] ERROR i.m.d.registration.AutoRegistration - Error occurred during service registration with Consul: Request decode failed: json: unknown field "ID"
io.micronaut.http.client.exceptions.HttpClientResponseException: Request decode failed: json: unknown field "ID"
    at io.micronaut.http.client.netty.DefaultHttpClient$12.channelRead0(DefaultHttpClient.java:2140)
    at io.micronaut.http.client.netty.DefaultHttpClient$12.channelRead0(DefaultHttpClient.java:2055)
    at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:99)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.micronaut.http.netty.stream.HttpStreamsHandler.channelRead(HttpStreamsHandler.java:193)
    at io.micronaut.http.netty.stream.HttpStreamsClientHandler.channelRead(HttpStreamsClientHandler.java:183)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)

控制器

@Controller("/api/${api.version:v1}/vendor")
public class ApiGatewayVendorController implements IVendorOperation{
 @Override
    public Maybe<?> get(VendorSearchCriteria searchCriteria) {
        return this.iVendorClient.get(searchCriteria);
    }
}

解决方法

我的问题是领事发现

consul:
  client:
    defaultZone: ${CONSUL_HOST:localhost}:${CONSUL_PORT:8500}
    registration:
      check:
        enabled: false
      enabled: true

添加以下代码是解决方法,但是,这是一个错误 https://github.com/micronaut-projects/micronaut-core/issues/3530

registration:
          check:
            enabled: false

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...