Display path modification of wagger

Transfer: https://www.jianshu.com/p/ce7e247515f5?utm_source=oschina-app

 

Note : This article is based on the springboot configuration, but in practice the use of springmvc is basically the same as the configuration of this article, which does not affect the use.

The following is the first configuration method. Minimize configuration as possible.
One
1 Introduce dependencies in the pom file

<!-- Swagger -->
<dependency>
    <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> 

2 Create a new swag configuration file
Define a new class in the code, the code is as follows

@Configuration
@EnableSwagger2
public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("对外开放接口API 文档") .description("HTTP对外开放接口") .version("1.0.0") .termsOfServiceUrl("http://xxx.xxx.com") .license("LICENSE") .licenseUrl("http://xxx.xxx.com") .build(); } } 

3 Use it in your own controller

@Slf4j
@RestController
public class MyController { @RequestMapping(value = "hello", method = RequestMethod.GET) public String hello() { return "hello"; } } 

The above configuration is enough. The following is the display effect.
Start the project locally, and then enter the browser:

The effect of http://localhost:8080/swagger-ui.html
is as follows:

 
swagger.png

 

This configuration method is very simple, and there is basically no intrusiveness to the original code. It can basically meet the needs of interface description.

However, there are many projects where the front-end and back-end are separated. In nginx, links starting with "/rest/" are configured to be sent to the back-end, while other requests are sent to the front-end. Of course, you can modify the configuration of nginx to send some links to the front end and the rest to the back end directly. However, this method will have certain security problems, and the controllability is not very good. It is best to modify the display address of swagger, for
example, from
http://localhost:8080/swagger-ui.html
to
http://localhost:8080/rest/api/doc/swagger-ui.html

The following is the second configuration method, you can customize the display link.
Two
1 Introduce dependencies in the pom file ( note that we removed the dependency on springfox-swagger-ui )

<!-- Swagger -->
<dependency>
    <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> 

2 git clone swagger-ui project

https://github.com/swagger-api/swagger-ui
Please select the version above 2.0 and below 3.0

Copy the dist folder to the resources/swagger directory in your own project, as shown in the figure


 
dist.png

3 Create a new swagger.properties file under resources, the contents of which are

springfox.documentation.swagger.v2.path=/rest/api/doc

4 Modify the index file under resources/swagger/dist and
put the

  <script type="text/javascript"> $(function () { var url = window.location.search.match(/url=([^&]+)/); if (url && url.length > 1) { url = decodeURIComponent(url[1]); } else { url = "http://petstore.swagger.io/v2/swagger.json"; } 

url = " http://petstore.swagger.io/v2/swagger.json "
is modified to
url = "/rest/api/doc"
after modification as follows

  <script type="text/javascript"> $(function () { var url = window.location.search.match(/url=([^&]+)/); if (url && url.length > 1) { url = decodeURIComponent(url[1]); } else { url = "/rest/api/doc"; } 

5 Create a new swag configuration file
Define a new class in the code, the code is as follows

@Configuration
@EnableSwagger2
@PropertySource("classpath:swagger.properties") // 新增对swagger.properties 的引入 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("对外开放接口API 文档") .description("HTTP对外开放接口") .version("1.0.0") .termsOfServiceUrl("http://xxx.xxx.com") .license("LICENSE") .licenseUrl("http://xxx.xxx.com") .build(); } } 

6 Add url mapping
If it is springboot, add it in Application

@SpringBootApplication
public class DemoApplication extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/rest/api/doc/**").addResourceLocations("classpath:/swagger/dist/"); } ..... } 

If it is springmvc, add it in api-servlet.xml (application.xml)

<mvc:resources mapping="/rest/api/doc/**" location="classpath:/swagger/dist/"/> 

Finally, the configuration is completed, start the project, and enter in the browser:

http://localhost:8080/rest/api/doc/index.html

 
swagger.png

 

springfox official network

http://springfox.github.io/springfox/

For the description of api annotations, please refer to

http://blog.csdn.net/xupeng874395012/article/details/68946676



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325218938&siteId=291194637