Swagger ui java maven

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Spring Boot Swagger Integration

JavaWiz/sb-swagger-ui

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Читайте также:  Html element for text

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Spring Boot RESTful API Documentation With Swagger 2

When creating a REST API, good documentation is instrumental.

Moreover, every change in the API should be simultaneously described in the reference documentation. Accomplishing this manually is a tedious exercise, so automation of the process was inevitable.

In this example, we will look at Swagger 2 for a Spring REST web service. For this example, we will use the Springfox implementation of the Swagger 2 specification.

Adding the Maven Dependency

As mentioned above, we will use the Springfox implementation of the Swagger specification. The latest version can be found on Maven Central.

To add it to our Maven project, we need a dependency in the pom.xml file.

 io.springfox springfox-swagger2 2.9.2  

Integrating Swagger 2 into the Project

@Configuration @EnableSwagger2 public class SwaggerConfig < @Bean public Docket api() < return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); >> 

Swagger 2 is enabled through the @EnableSwagger2 annotation.

After the Docket bean is defined, its select() method returns an instance of ApiSelectorBuilder, which provides a way to control the endpoints exposed by Swagger.

Predicates for selection of RequestHandlers can be configured with the help of RequestHandlerSelectors and PathSelectors. Using any() for both will make documentation for your entire API available through Swagger.

This configuration is enough to integrate Swagger 2 into an existing Spring Boot project. For other Spring projects, some additional tuning is required.

To verify that Springfox is working, you can visit the following URL in your browser:

The result is a JSON response with a large number of key-value pairs, which is not very human-readable. Fortunately, Swagger provides Swagger UI for this purpose.

Swagger UI is a built-in solution which makes user interaction with the Swagger-generated API documentation much easier.

Enabling Springfox’s Swagger UI

To use Swagger UI, one additional Maven dependency is required:

 io.springfox springfox-swagger-ui 2.9.2  

Now you can test it in your browser by visiting http://localhost:8080/swagger-ui.html

In our case, by the way, the exact URL will be: http://localhost:8080/swagger-ui.html

Exploring Swagger Documentation

Within Swagger’s response is a list of all controllers defined in your application. Clicking on any of them will list the valid HTTP methods (DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT).

Expanding each method provides additional useful data, such as response status, content-type, and a list of parameters. It is also possible to try each method using the UI.

About

Spring Boot Swagger Integration

Источник

How to integrate swagger with maven + java + jersey +tomcat?

Swagger is a popular open-source framework for API documentation and testing. Integrating Swagger with a Java project that uses Maven, Jersey, and Tomcat can help you generate and access API documentation easily. This can also be helpful for testing the API endpoints. In this article, we will look at different methods for integrating Swagger into a Java project with Maven, Jersey, and Tomcat.

Method 1: Using Swagger Core

Here are the steps to integrate Swagger with Maven + Java + Jersey + Tomcat using Swagger Core:

build> plugins> plugin> groupId>com.github.kongchengroupId> artifactId>swagger-maven-pluginartifactId> version>3.1.8version> executions> execution> phase>compilephase> goals> goal>generategoal> goals> configuration> apiSources> apiSource> springmvc>falsespringmvc> locations> location>your.package.name.to.scan.for.swagger.annotationslocation> locations> schemes> scheme>httpscheme> scheme>httpsscheme> schemes> host>localhost:8080host> basePath>/your-app-context-rootbasePath> info> title>Your API Titletitle> version>Your API Versionversion> description>Your API Descriptiondescription> info> apiSource> apiSources> outputDirectory>$/generated-sources/swaggeroutputDirectory> swaggerDirectory>$/swagger-uiswaggerDirectory> templatePath>$/src/main/resources/swagger-templatetemplatePath> attachSwaggerArtifact>trueattachSwaggerArtifact> swaggerApiReader>com.github.kongchen.swagger.docgen.reader.JaxrsReaderswaggerApiReader> configuration> execution> executions> plugin> plugins> build>
dependencies> dependency> groupId>io.swaggergroupId> artifactId>swagger-coreartifactId> version>1.5.0version> dependency> dependency> groupId>io.swaggergroupId> artifactId>swagger-jaxrsartifactId> version>1.5.0version> dependency> dependency> groupId>io.swaggergroupId> artifactId>swagger-jersey-jaxrsartifactId> version>1.5.0version> dependency> dependency> groupId>org.webjarsgroupId> artifactId>swagger-uiartifactId> version>3.25.0version> dependency> dependencies>
servlet> servlet-name>Jersey2Configservlet-name> servlet-class>org.glassfish.jersey.servlet.ServletContainerservlet-class> init-param> param-name>jersey.config.server.provider.packagesparam-name> param-value>your.package.name.to.scan.for.jersey.resourcesparam-value> init-param> init-param> param-name>jersey.config.server.provider.classnamesparam-name> param-value>io.swagger.jaxrs.listing.ApiListingResourceparam-value> init-param> load-on-startup>1load-on-startup> servlet> servlet-mapping> servlet-name>Jersey2Configservlet-name> url-pattern>/api/*url-pattern> servlet-mapping> servlet> servlet-name>SwaggerBootstrapservlet-name> servlet-class>com.github.kongchen.swagger.bootstrap.ui.SwaggerBootstrapUiservlet-class> init-param> param-name>com.github.kongchen.swagger.docs.scan.PackageToScanparam-name> param-value>your.package.name.to.scan.for.swagger.annotationsparam-value> init-param> load-on-startup>2load-on-startup> servlet> servlet-mapping> servlet-name>SwaggerBootstrapservlet-name> url-pattern>/swagger/*url-pattern> servlet-mapping>
import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/hello") @Api(value = "/hello", description = "Hello world API") public class HelloWorldResource  @GET @ApiOperation(value = "Say hello", notes = "Returns a greeting message") @Produces(MediaType.TEXT_PLAIN) public String sayHello()  return "Hello, world!"; > >
http://localhost:8080/your-app-context-root/swagger/index.html

That’s it! Now you have successfully integrated Swagger with Maven + Java + Jersey + Tomcat using Swagger Core.

Method 2: Using Swagger UI

Integrating Swagger with Maven, Java, Jersey, and Tomcat using Swagger UI

Here are the steps to integrate Swagger with Maven, Java, Jersey, and Tomcat using Swagger UI:

dependency> groupId>io.swaggergroupId> artifactId>swagger-jersey2-jaxrsartifactId> version>2.1.1version> dependency> dependency> groupId>io.swaggergroupId> artifactId>swagger-coreartifactId> version>2.1.1version> dependency>
swagger: "2.0" info: version: "1.0.0" title: "My API" basePath: "/api" schemes: - "http" consumes: - "application/json" produces: - "application/json"
servlet> servlet-name>Swagger Servletservlet-name> servlet-class>io.swagger.jersey.config.JerseyJaxrsConfigservlet-class> init-param> param-name>api.versionparam-name> param-value>1.0.0param-value> init-param> init-param> param-name>swagger.api.basepathparam-name> param-value>/apiparam-value> init-param> load-on-startup>2load-on-startup> servlet> servlet-mapping> servlet-name>Swagger Servletservlet-name> url-pattern>/api/*url-pattern> servlet-mapping>
DOCTYPE html> html> head> title>Swagger UItitle> link rel="stylesheet" type="text/css" href="/api/swagger-ui.css"> script src="/api/swagger-ui-bundle.js"> script> script src="/api/swagger-ui-standalone-preset.js"> script> head> body> div id="swagger-ui">div> script> window.onload = function()  // Build a system const ui = SwaggerUIBundle( url: "/api/swagger.json", dom_id: "#swagger-ui", presets: [ SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset ], layout: "StandaloneLayout" >) > script> body> html>
import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/hello") @Api(value = "/hello", description = "Hello world API") public class HelloResource  @GET @Produces(MediaType.TEXT_PLAIN) @ApiOperation(value = "Get hello world message", response = String.class) public String getHello()  return "Hello world!"; > >
  1. Build and deploy your application to Tomcat.
  2. Access the Swagger UI at http://localhost:8080/your-app-name/ .

That’s it! You should now be able to use Swagger to document and test your API.

Method 3: Using the Swagger Maven Plugin

To integrate Swagger with Maven, Java, Jersey, and Tomcat, we can use the Swagger Maven Plugin. This plugin generates Swagger documentation for our RESTful API and provides a UI to interact with the API. Here are the steps to integrate Swagger with Maven, Java, Jersey, and Tomcat using the Swagger Maven Plugin:

Step 1: Add the Swagger Maven Plugin to your pom.xml file

build> plugins> plugin> groupId>com.github.kongchengroupId> artifactId>swagger-maven-pluginartifactId> version>3.1.8version> configuration> apiSources> apiSource> locations>com.example.package.to.scanlocations> schemes>http,httpsschemes> host>localhost:8080host> basePath>/apibasePath> info> title>API Titletitle> version>1.0.0version> description>API Descriptiondescription> info> apiSource> apiSources> configuration> plugin> plugins> build>

Step 2: Generate Swagger documentation

Run the following command to generate Swagger documentation:

mvn clean compile swagger:generate

Step 3: Add Swagger UI to your project

Add the following dependencies to your pom.xml file:

dependency> groupId>org.webjarsgroupId> artifactId>swagger-uiartifactId> version>3.52.3version> dependency> dependency> groupId>org.webjarsgroupId> artifactId>webjars-locator-coreartifactId> version>0.45version> dependency>

Step 4: Configure Swagger UI

Add the following servlet to your web.xml file:

servlet> servlet-name>Swagger UIservlet-name> servlet-class>org.webjars.swagger.ui.SwaggerUiServletservlet-class> init-param> param-name>swagger.api.basepathparam-name> param-value>/apiparam-value> init-param> load-on-startup>2load-on-startup> servlet> servlet-mapping> servlet-name>Swagger UIservlet-name> url-pattern>/swagger/*url-pattern> servlet-mapping>

Step 5: Run your project

Run your project and navigate to http://localhost:8080/swagger/index.html to access the Swagger UI.

That’s it! Now you have integrated Swagger with Maven, Java, Jersey, and Tomcat using the Swagger Maven Plugin.

Источник

Оцените статью