SSM框架,springMVC集成Swagger2

时间:2019-04-13
本文章向大家介绍SSM框架,springMVC集成Swagger2,主要包括SSM框架,springMVC集成Swagger2使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

环境:

spring + springMVC + mybatis + eclipse + jdk1.8

 

1.整合框架

参考:https://blog.csdn.net/weixin_42425970/article/details/89055615 完整整合过程

2.导入依赖(maven)

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

3.创建SwaggerConfig类,创建在哪里都可以

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
 
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
@EnableSwagger2 //使swagger2生效  
@ComponentScan(basePackages = {"com.hc.travelers.web.controller"}) //需要扫描的controller包路径  
@Configurable //配置注解,自动在本类上下文加载一些环境变量信息  
public class SwaggerConfig extends WebMvcConfigurationSupport {
	//RestApiConfig 
	   @Bean
	    public Docket buildDocket(){
	        return new Docket(DocumentationType.SWAGGER_2)
	                .apiInfo(buildApiInf())
	                .select()       
	                .apis(RequestHandlerSelectors.basePackage("com.hc.travelers.web.controller"))//controller路径
	                .paths(PathSelectors.any())
	                .build();
	    }
 
	    private ApiInfo buildApiInf(){
	        return new ApiInfoBuilder()
	                .title("这是我第一次成功使用swagger")
	                .termsOfServiceUrl("http://www.baidu.com")
	                .description("springmvc swagger2")
	                .contact(new Contact("杨鑫虎",
	                		"https://blog.csdn.net/zhang289202241/article/details/78553318",
	                		"naivestruggle@163.com"))
	                .build();
 
	    }
}

注意扫描的包是自己写的controller所在的包。

4.配置xml文件

添加applicationContext-swagger.xml文件(这个文件其实就是beans.xml文件,如不理解参考整合框架过程https://blog.csdn.net/weixin_42425970/article/details/89055615 )

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
			http://www.springframework.org/schema/beans 
			http://www.springframework.org/schema/beans/spring-beans.xsd
			http://www.springframework.org/schema/util 
			http://www.springframework.org/schema/util/spring-util.xsd
			http://www.springframework.org/schema/context 
			http://www.springframework.org/schema/context/spring-context.xsd
			http://www.springframework.org/schema/tx 
			http://www.springframework.org/schema/tx/spring-tx.xsd
			http://www.springframework.org/schema/mvc 
			http://www.springframework.org/schema/mvc/spring-mvc.xsd
			http://www.springframework.org/schema/aop 
			http://www.springframework.org/schema/aop/spring-aop.xsd"> 
<!-- 加载SwaggerConfig  类名改成自己创建的-->
<bean class="com.common.utils.swagger.SwaggerConfig"/>
</beans>

 

5.在自己的controller包下创建DemoController.java


import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Controller
public class DemoController {

	//@ApiIgnore忽略这个参数   就是在swagger网站中测试时不用填写这个参数
	@PostMapping("getNameById")
	@ApiOperation(value="获取用户姓名",notes = "获取用户信息  这是notes描述",httpMethod = "POST")
	private void getNameById(String userId,@ApiIgnore HttpServletResponse response) throws IOException{
		response.getWriter().append("my name is douniwan");
	}
}

6.运行

启动服务器

用浏览器直接访问:http://127.0.0.1:8080/{你的项目名}/swagger-ui.html 中间项目名写自己的。

访问结果:

 

点击即可进入测试。

 

 

至此,ssm框架简单使用swagger生成接口文档配置完毕。