2

Not sure if this is the right place to ask. But I can not find anything in the documentation or with the search-(engine).

I am trying to set up a small server for the generated swagger classes with an embedded jetty server. So far this works with the following configuration of Jetty:

ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/path");

QueuedThreadPool threadPool = getThreadPool();

jettyServer = new Server(threadPool);
jettyServer.setHandler(context);

// Shutdown gracefully
jettyServer.setStopAtShutdown(true);
// Set time until server get killed.
// jettyServer.setStopTimeout(30);

// Add a http config and a connector to set the port manually 
HttpConfiguration httpConfig = new HttpConfiguration();
ServerConnector http = new ServerConnector(jettyServer, new HttpConnectionFactory(httpConfig));
http.setPort(8080);

jettyServer.addConnector(http);

ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);

// Scans the package for new jersey controllers
jerseyServlet.setInitParameter(ServerProperties.PROVIDER_PACKAGES,
        "ch.company.ilp.clag.server.api" + "," + "ch.company.ilp.clag.controller" + ", " + "io.swagger.jaxrs.listing");

jerseyServlet.setInitParameter("jersey.config.server.wadl.disableWadl", "true");
// Finally start the server
jettyServer.start();

The calls to the API works as expected. Also, my "custom filter" works without any problem.

To build the response in the body I use the following code:

// this object is annotated with the jackson annotations. 
Response.status(Status.OK).entity(swaggerResponseObject).build()

I noticed when I do not set a value in the swagger objects the API returns them as null.

Example json response:

Response:

"global_response": {
    "result_state": "OK",
    "result_message": "",
    "result_code": "RESULT_STATE_OK",
    "result_attributes": null,
    "localizable_code": null
}

Expectation:

"global_response": {
    "result_state": "OK",
    "result_message": "",
    "result_code": "RESULT_STATE_OK",
}
  1. Is this the default behavior with swagger and jaxrs?
  2. Is this configurable?
  3. How?

I think something is wrong with how I try to load the classes / packages to my servlet. As described in this question: Null values on swagger JSON file

With further searching I found out that the SwaggerSerializers.writeTo() never gets invoked. Instead the ProviderBase.writeTo() gets invoked.

No classes from the package: io.swagger.jaxrs.listing is called.

Any tips are welcome.

Thank you!

NeXT405
  • 55
  • 5

1 Answers1

2

Depending on JSON serializer you use, you need to configure null handling in a proper way.

In a jackson it's done by:

@JsonInclude(JsonInclude.Include.NON_NULL)
class YourClass{
   ...
}

Or if you want this behavior globally, configure your ObjectMapper:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);