1.Adding the Maven DependencyAs mentioned above, we'll use the Springfox implementation of the Swagger specification. To add it to my Maven project, we need a dependency in the Pom.xml file.
<dependency>
<groupId>
Io.springfox
</groupId>
<artifactId>
Springfox-swagger2
</artifactId>
<version>
2.7.0
</version>
</dependency>
2.Integrating Swagger 2 into the Project
2.1.Java Configuration The configuration of Swagger mainly centers around the docket Bean.
Swagger 2 is enabled through the@EnableSwagger2Annotation. After the docket bean was defined, itsSelect ()method returns an instance ofApiselectorbuilder, which provides a-to-control the endpoints exposed by Swagger. Predicates for selection of RequestHandler s can is configured with the help ofrequesthandlerselectorsandpathselectors. Using any () for both would make documentation for your entire API available through Swagger. This configuration was enough to integrate Swagger 2 into existing Spring Boot project. For other Spring projects, some additional tuning is required.2.2.Configuration without Spring BootWithout Spring Boot, you don't have the luxury of auto-configuration of your resource handlers. Swagger UI adds a set of resources which you must configure as part of a class that extends Webmvcconfigureradapter, and is annotated with @EnableWebMvc.
2.3.VerificationTo verify that Springfox are working, you can visit the following URLs in your Browser:http://localhost:8080/spring-securit Y-rest/api/v2/api-docs The result is a JSON response with a large number of key-value pairs, which are not very human-reada ble. Fortunately, Swagger providesSwagger UIFor this purpose.3.Swagger UISwagger UI is a built-in solution which makes user interaction with the swagger-generated API documentation much easier.3.1.Enabling Springfox ' s Swagger UITo use the Swagger UI, one additional Maven dependency is required:
Now the can test it in your browser by visiting http://localhost:8080/swagger-ui.htmlIn we case , by the the-the-by, the exact URL would be: http://localhost:8080/swagger-ui.html The result should look something:
3.2.Exploring Swagger DocumentationWithin Swagger ' s response is alist of all controllersdefined in your application. Clicking on them would 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. Swagger ' s ability to being synchronized with your code base is crucial. To demonstrate this, you can add a new controller to your application.
Now, if you refresh the Swagger documentation, you'll seeCustom-controllerIn the list of controllers. As you know, there are only one method (POST) shown in Swagger ' s response.4. Advanced ConfigurationThe docket bean of your application can be configured to give-more control over the API documentation Generation Pro Cess.4.1.Filtering API for Swagger ' s ResponseIt is a desirable to expose the documentation for your entire API. You can restrict Swagger ' s response by passing parameters to theAPIs ()andpaths ()Methods of the Docket class. As seen above, Requesthandlerselectors allows using the or none predicates, but can also is used to filter the A PI according to the base package, class annotation, and method annotations.pathselectorsProvides additional filtering with predicates which scan the request paths of your application. You can use any (), none (), Regex (), or ant (). In the example below, we'll instruct Swagger to include only controllers from a particular package with specific paths, Using the ant () predicate.
4.2.Custom InformationSwagger also provides some default values in it response which you can customize, such as "Api Documentation", "Created b Y contact Email "," Apache 2.0 ". To change these values, you can use theApiinfo (apiinfo apiinfo)Method. TheApiinfoClass that contains custom information about the API.
4.3.Custom Methods Response MessagesSwagger allowsglobally overriding response messages of HTTP methodsThrough docket ' sglobalresponsemessage () Method. First, you must instruct Swagger is not the use default response messages. Suppose wish to override -and403Response messages for all GET methods. To achieve this, some code must being added to the docket ' s initialization block (original code is excluded for clarity):
5.ConclusionIn this tutorial, we set up Swagger 2 to generate documentation for a Spring REST API. We also has explored ways to visualize and customize Swagger ' s output. TheFull ImplementationOf this tutorial can being found in the Github Project–this are an Eclipse based project, so it should being easy to import a nd run as it is. And, if you're a student of REST with Spring, go-to-Lesson 1 from-Module 7 for a deep-dive-setting up Swagger with Spring and Spring Boot.
6.The Original Address
Http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api
7.Source code Download address Https://gitee.com/micai/micai-springmvc-swagger