0) add Maven dependencies:
com.wordnik:swagger-jaxrs_2.10:1.3.1
1) with an Application, register the Swagger jaxrs resources:
import java.util.Set; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; @ApplicationPath("/rest") public class JaxRsActivator extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> resources = new java.util.HashSet<>(); resources.add(com.wordnik.swagger.jaxrs.listing.ApiListingResource.class); resources.add(com.wordnik.swagger.jaxrs.listing.ApiDeclarationProvider.class); resources.add(com.wordnik.swagger.jaxrs.listing.ApiListingResourceJSON.class); resources.add(com.wordnik.swagger.jaxrs.listing.ResourceListingProvider.class); // resources.add(BearerTokenFilter.class); addRestResourceClasses(resources); return resources; } private void addRestResourceClasses(Set<Class<?>> resources) { resources.add(BlaService.class); resources.add(MumbleService.class); } }
2) register the Swagger configuration:
import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import com.wordnik.swagger.config.ConfigFactory; import com.wordnik.swagger.config.ScannerFactory; import com.wordnik.swagger.config.SwaggerConfig; import com.wordnik.swagger.jaxrs.config.DefaultJaxrsScanner; import com.wordnik.swagger.jaxrs.reader.DefaultJaxrsApiReader; import com.wordnik.swagger.reader.ClassReaders; @WebServlet(name = "SwaggerJaxrsConfig", loadOnStartup = 1) public class SwaggerJaxrsConfig extends HttpServlet { @Override public void init(ServletConfig servletConfig) { try { super.init(servletConfig); SwaggerConfig swaggerConfig = new SwaggerConfig(); ConfigFactory.setConfig(swaggerConfig); swaggerConfig.setBasePath("/rest"); swaggerConfig.setApiVersion("2.1"); ScannerFactory.setScanner(new DefaultJaxrsScanner()); ClassReaders.setReader(new DefaultJaxrsApiReader()); } catch (ServletException e) { System.out.println(e.getMessage()); } } }
3) then you can annotare your services methods:
import javax.inject.Inject; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.HeaderParam; import javax.ws.rs.NotAuthorizedException; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.wordnik.swagger.annotations.Api; import com.wordnik.swagger.annotations.ApiOperation; import com.wordnik.swagger.annotations.ApiParam; import com.wordnik.swagger.annotations.ApiResponse; import com.wordnik.swagger.annotations.ApiResponses; @Path("/pippo") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) @Api(value = "/pippo", description = "REST service end-points exposed to Pippo") public class PippoService { @GET @Path("/profiles") @Produces(MediaType.APPLICATION_JSON) @ApiOperation(value = "All Pippos", notes = "List all Pippos") @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 500, message = "Server error, check log files") }) public ListlistPippobyQuery( @ApiParam(value = "the hostname where Pippo resides") @QueryParam("hostname") String hostName) { return listPippo(hostName); } }
1 comment:
Where is the generated JSON or where can I check the generated documentation?
Post a Comment