SSM框架,springMVC集成Swagger2

3 篇文章 0 订阅
2 篇文章 0 订阅

环境:

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生成接口文档配置完毕。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值