3

Micronuat default configuration for JSON serialization is JsonInclude.Include.NON_EMPTY which doesn't include an empty or null value. However, I have a requirement to include the empty and null value, so what I did so far -

@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;
    }
}

Pagination model

@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
}

When I add @JsonInclude(ALWAYS) it doesn't include the NULL or EMPTY value in the JSON response as shown below.

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

If I add the below code, it does include the NULL or EMPTY value for lastId and vendors

jackson:
  serializationInclusion: ALWAYS

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

However, it throws an exception as below

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

@Controller("/api/${api.version:v1}/vendor")
public class ApiGatewayVendorController implements IVendorOperation{
 @Override
    public Maybe<?> get(VendorSearchCriteria searchCriteria) {
        return this.iVendorClient.get(searchCriteria);
    }
}
San Jaisy
  • 15,327
  • 34
  • 171
  • 290

1 Answers1

0

The issue for me was with the consul discovery

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

Adding the below code is the workaround, however, this is a bug https://github.com/micronaut-projects/micronaut-core/issues/3530

registration:
          check:
            enabled: false
San Jaisy
  • 15,327
  • 34
  • 171
  • 290