2

JSON 序列化的 Micronuat 默认配置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
}

如果我添加下面的代码,它确实包含NULLEMPTYlastIdvendors

jackson:
  serializationInclusion: ALWAYS

{
    "pageSize": 10,
    "total": 0,
    "currentPage": 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);
    }
}
4

1 回答 1

1

对我来说,问题在于领事发现

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
于 2021-01-18T09:53:52.540 回答