I have a JAX-RS
web service which I want to document with Swagger 2.1
.
The config is build in my Servlet:
public class FooWebservice extends HttpServlet {
@Override
public void init(ServletConfig config) throws ServletException {
OpenAPI oas = new OpenAPI();
Info info = new Info()
.title("Foo-Webservice")
.version("1.0.0");
oas.info(info);
SwaggerConfiguration oasConfig = new SwaggerConfiguration()
.prettyPrint(true)
.openAPI(oas)
.resourcePackages(Stream.of("de.kembytes.foo.webservice.controller").collect(Collectors.toSet()));
try {
new JaxrsOpenApiContextBuilder()
.servletConfig(config)
.openApiConfiguration(oasConfig)
.buildContext(true);
} catch (OpenApiConfigurationException e) {
throw new ServletException(e.getMessage(), e);
}
}
}
In addition I have a controller that defines the operation (in package de.kembytes.foo.webservice.controller
):
@Path("/foo")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Tag(name = "Foo")
public class FooController {
@POST
@Path("/calculate")
@Operation(summary = "returns bar",
responses = {
@ApiResponse(responseCode = "200", description = "bar", content = {
@Content(mediaType = MediaType.APPLICATION_XML, schema = @Schema(implementation = Bar.class)),
@Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = Bar.class)) }) })
public Bar calculate(@RequestBody(required = true, content = @Content(schema = @Schema(implementation = FooInput.class))) FooInput input) throws Exception {
Bar bar = new Bar();
bar.setValue1(...);
bar.setValue2(...);
bar.setValue3(...);
return bar;
}
}
When i start my Application and get the OpenApi-Documentation the FooController
is not included. It looks like this:
{
"openapi" : "3.0.1",
"info" : {
"title" : "Foo-Webservice",
"version" : "1.0.0"
}
}
Why is it that the config was not loaded in FooController
although it is in the specified resource package?
I have solved this problem by scanning my package for all classes with the annotation @Path
with the Reflections library . Then i set all of them as resourceClasses
.
The init method looks like this now:
@Override
public void init(ServletConfig config) throws ServletException {
OpenAPI oas = new OpenAPI();
Info info = new Info()
.title("Foo-Webservice")
.version("1.0.0");
oas.info(info);
Set<String> resourceClasses = new Reflections(getClass().getPackageName())
.getTypesAnnotatedWith(Path.class)
.stream().map(c -> c.getName())
.collect(Collectors.toSet());
SwaggerConfiguration oasConfig = new SwaggerConfiguration()
.prettyPrint(true)
.openAPI(oas)
.resourceClasses(resourceClasses);
try {
new JaxrsOpenApiContextBuilder()
.servletConfig(config)
.openApiConfiguration(oasConfig)
.buildContext(true);
} catch (OpenApiConfigurationException e) {
throw new ServletException(e.getMessage(), e);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.