Swagger+Spring+Spring Mvc项目整合DEMO

本文介绍如何使用Swagger为RESTful API生成文档并提供测试功能。通过具体步骤演示如何在Spring MVC项目中集成Swagger,包括依赖配置、Swagger配置及前端界面设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.简介

  Swagger是一个简单又强大的能为你的Restful风格的Api生成文档工具,可以自动生成页面形式的Api文档,并且提供测试的功能,Swagger使用起来方便、快捷、降低前后端以及测试的成本,有十分风骚且美观的界面 供测试和查看。


2.缺点

  Swagger嵌入在代码中,依赖强,容易污染代码,大型项目作用性不强(注解太多)若中途使用swagger成本较高。

3.上代码,聊人生

 声明:本文章采用spring+spring mvc+swagger框架,数据方面模拟,其他多余东西没有。

  3.1 创建Maven项目-导入依赖

   
		
    
		
    
			
     
      com.mangofactory
     
			
     
      swagger-springmvc
     
			
     
      1.0.2
     
		
    
		
    
		
    
			
     
      javax.servlet
     
			
     
      javax.servlet-api
     
			
     
      3.1.0
     
			
     
      provided
     
		
    
		
    
			
     
      javax.servlet
     
			
     
      jstl
     
			
     
      1.2
     
		
    
		
    
			
     
      com.fasterxml.jackson.core
     
			
     
      jackson-databind
     
			
     
      2.6.6
     
		
    
		
    
			
     
      org.springframework
     
			
     
      spring-webmvc
     
			
     
      4.1.6.RELEASE
     
		
    
		
    
			
     
      org.springframework
     
			
     
      spring-web
     
			
     
      4.1.6.RELEASE
     
		
    
		
    
			
     
      org.springframework
     
			
     
      spring-tx
     
			
     
      4.1.6.RELEASE
     
		
    
		
    
			
     
      org.springframework
     
			
     
      spring-jdbc
     
			
     
      4.1.6.RELEASE
     
		
    
		
    
			
     
      org.springframework
     
			
     
      spring-context
     
			
     
      4.1.6.RELEASE
     
		
    
		
    
			
     
      org.springframework
     
			
     
      spring-context-support
     
			
     
      4.1.6.RELEASE
     
		
    
		
    
			
     
      org.springframework
     
			
     
      spring-orm
     
			
     
      4.1.6.RELEASE
     
		
    
		
    
			
     
      org.springframework
     
			
     
      spring-aop
     
			
     
      4.1.6.RELEASE
     
		
    
		
    
			
     
      org.springframework
     
			
     
      spring-oxm
     
			
     
      4.1.6.RELEASE
     
		
    
		
    
			
     
      org.springframework
     
			
     
      spring-test
     
			
     
      4.1.6.RELEASE
     
			
     
      test
     
		
    
		
    
		
    
			
     
      org.slf4j
     
			
     
      slf4j-log4j12
     
			
     
      1.7.7
     
		
    
		
    
	
   
  3.2 配置spring+spring mvc 

     

     

  3.2 配置web.xml
    
     
		
      
       contextConfigLocation
      
		
      
       
             classpath:spring-config.xml
         
      
	
     
	
     
		
      
       org.springframework.web.context.ContextLoaderListener
      
	
     

	
     
		
      
       springmvc
      
		
      
       org.springframework.web.servlet.DispatcherServlet
      
		
      
			
       
        contextConfigLocation
       
			
       
        classpath:spring-mvc.xml
       
		
      
		
      
       1
      
	
     
	
     
		
      
       springmvc
      
		
      
       /
      
	
     
  3.3 Swagger配置信息

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.models.dto.ApiInfo;
import com.mangofactory.swagger.paths.SwaggerPathProvider;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;

@Configuration
@EnableWebMvc
@EnableSwagger
public class SwaggerPluginConfig extends WebMvcConfigurerAdapter {

	private SpringSwaggerConfig springSwaggerConfig;

	@Bean
	public SwaggerSpringMvcPlugin customImplementation() {
		return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
				.apiInfo(apiInfo())
				.includePatterns(".*")
				.useDefaultResponseMessages(false)
				// .pathProvider(new GtPaths())
				.apiVersion("0.1")
				.swaggerGroup("user");

	}

	private ApiInfo apiInfo() {
		ApiInfo apiInfo = new ApiInfo("这是标题-Laher", "这是描述-Laher",
				"服务条款路径", "weisheixiaoxin@qq.com", "Laher博客",
				"http://blog.csdn.net/weisheixiaoxin");
		return apiInfo;
	}

	@Override
	public void configureDefaultServletHandling(
			DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}

	class GtPaths extends SwaggerPathProvider {
		@Override
		protected String applicationPath() {
			return "/restapi";
		}

		@Override
		protected String getDocumentationPath() {
			return "/restapi";
		}
	}

	@Autowired
	public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {
		this.springSwaggerConfig = springSwaggerConfig;
	}
}
  3.4 Controller中使用注释声明节点
//swagger的节点声明,节点名称,类型。
@Api(value = "user", description = "用户管理", produces = MediaType.APPLICATION_JSON_VALUE)
@Controller
@RequestMapping("user")
public class UserController
  3.5 接口方法注释声明
//swagger的节点声明,节点名称,类型。
@ApiOperation(value = "查询用户", notes = "查询用户信息", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
//spring mvc注解声明
@ResponseBody
@RequestMapping(value = "get", method = RequestMethod.GET)
//                          -swagger声明入参变量的英文和是否空         -spring mvc入参声明 这个一定要加
public Result
        
          get(@ApiParam(value = "编号", required = true) @RequestParam String id) {
	return new Result
         
          (0000, "操作成功", getUser(id));
}
         
        
----后端-配置完成----------------------------------------------------------------------------------   
  3.6.配置前端
声明:前端页面需要使用swagger-ui,所以需要提前下载资源

下载完毕后有一堆的文件,进入   dist 目录下面
拷贝文件:
swagger-ui.min.js
swagger-ui.js
\lib目录
\images目录
\css目录
\index.html页面
拷贝完成后终于快结束了%>_<%。。。打开index.html 配置成自己的路径————ok!!!

----前端-配置完成----------------------------------------------------------------------------------

4.启动项目-案例图

  

打开 /user/get api接口


说明:
id 入参变量名
try it out! 点击测试运行



说明:
  请求路径,响应内容,响应状态码,响应头部信息 信息一目了然。  

  不同的请求、操作、入参、出参、类型等都会有不同的页面格式信息,该demo提供了基本的操作。

5.参考资料

官网: 

技术支持:

demo:

---------------------------------------------------------------------------------------------------------


十分感谢各位的阅读与支持 ! !

谢谢你们 ! !




---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值