SSM project integration Swagger

According to the needs, I need to integrate Swagger in the SSM project. The swagger I use here is version 2.0 or higher.
1. First, we must introduce dependencies. It should be noted that if the swagger version does not match the spring version, an error 500 may be reported when accessing swagger. At this time, you need to lower the swagger version in the pom file or increase the spring version. Sample code:

<!--springfox的核心jar包-->
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.6.1</version>
</dependency>
<!--springfox-ui的jar包(里面包含了swagger的界面静态文件)-->
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>2.6.1</version>
</dependency>
   
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
 <groupId>com.fasterxml.jackson.core</groupId>
 <artifactId>jackson-databind</artifactId>
 <version>2.9.0</version>
 <!-- <version>2.9.5</version> -->
</dependency>

<!-- Google的Java常用类库 -->
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>26.0-jre</version>
</dependency>

<!-- web端静态资源的jar包 -->
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>bootstrap</artifactId>
    <version>3.3.5</version>
</dependency>

2. Write swagger tool class, sample code:

package com.gx.util;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/*
 * 重要!如果你的项目引入junit测试,此处需要使用@WebAppConfiguration,如果没有使用junit使用@Configuration
 */
@Configuration
@EnableSwagger2 // 重要!
//@EnableWebMvc
@ComponentScan(basePackages = {
    
     "com.gx.web" }) // 扫描control所在的package请修改为你control所在package
// 如果要访问的话直接访问 http://localhost:8080/项目名称/swagger-ui.html 就可以访问到文档了
public class SwaggerConfig {
    
    
	@Bean
	public Docket api() {
    
    
		return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()//选择哪些路径和API会生成document 
                //扫描指定包中的swagger注解
                //.apis(RequestHandlerSelectors.basePackage("com.gx.web"))
                //扫描所有有注解的api,用这种方式更灵活
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //扫描所有的api(没有添加注解也可以扫描出来),用这种方式更直接
                //.apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
	}

	/**
	 * 这是匹配api的信息
	 * 
	 * @return
	 */
	private ApiInfo apiInfo() {
    
    
		return new ApiInfoBuilder()
				// 大标题
				.title("API接口文档")
				// 描述
				.description("API接口测试")
				// 版本号
				.version("1.0.0")
				.termsOfServiceUrl("").license("").licenseUrl("").build();
	}
}

3. Configure swagger in the springMVC configuration file, sample code:

<!--将静态资源交由默认的servlet处理-->
<mvc:default-servlet-handler />
<!--向容器自动注入配置-->
<context:annotation-config />
<!--自动扫描,使springMVC认为包下用了@controller注解的类是控制器-->
<context:component-scan base-package="com.gx.web"/>
<!--重要!将你的SwaggerConfig配置类注入-->
<bean class="com.gx.util.SwaggerConfig"/>
<!--重要!配置swagger资源不被拦截-->
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />

4. Modify the configuration in the web.xml file, so that all requests are processed by DispatcherServlet, sample code:

<servlet-mapping>
   <servlet-name>springMvc</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping>

5. Add swagger annotations @Api and @ApiOperation to the controller. @Api() is used for classes-indicating that this class is a swagger resource, @ApiOperation() is used for methods-indicating an http request operation, sample code :

@Controller
@RequestMapping("/user")
@Api(value = "用户接口", description = "用户相关api")
public class UserController {
    
    

	@Autowired
	private IUserService userService;

	// 登录页面
	@RequestMapping("/loginsPage")
	@ApiOperation(value = "登录", notes = "后台登录",httpMethod = "GET", response = ModelAndView.class)
	public ModelAndView loginsPage() {
    
    
		ModelAndView mv = new ModelAndView("/login");
		return mv;
	}
}

6. Run the project (I use the local Tomcat to run the project here), visit in the browser, the effect screenshot:
visit address: http://localhost:8080/project name/swagger-ui.html
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44547592/article/details/108959016