SSM框架整合Swagger2(非maven项目者进)

首先网上已经有很多关于springMVC整合swagger2的帖子了,但是本人的项目并不是maven的项目,可能银行业干久了,框架什么的还是比较久,但是我相信国内还是很多项目都还不是maven的项目。(当然会存在历史问题的项目的嘛)
好了废话不多说了,下面是关于SSM框架整合swagger2的步骤,希望对大家有所帮助。
1)引入swagger一系列关联的包,原来的SSM框架的包保留,额外引入下面的包,如果已经存在相应的包就不用重复引入,别以为包多不压身,会出现包冲突问题的,那时候又要哭去了。
SSM框架整合Swagger2(非maven项目者进)_第1张图片
SSM框架整合Swagger2(非maven项目者进)_第2张图片
2)新增一个包,下面放swagger的一个配置类。此地方我命名为SwaggerConfig.java
package com.yusys.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

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;


@Configuration //配置注解
@EnableSwagger2 //启用swagger2
@EnableWebMvc //非springboot框架需要引入
@ComponentScan(basePackages = {"com.yusys.swagger"}) //扫描包
public class SwaggerConfig extends WebMvcConfigurerAdapter{
	
	private String version = "1.0";
	@Bean
	public Docket api(){
		return new Docket(DocumentationType.SWAGGER_2)
		.groupName("business-api")
		.genericModelSubstitutes(DeferredResult.class)
		.forCodeGeneration(false)
		.select()// 选择哪些路径和API会生成document
		.apis(RequestHandlerSelectors.any())// 对所有api进行监控
		.paths(PathSelectors.any())// 对所有路径进行监控
		.build()
		.apiInfo(apiInfo());
	}
	
	private ApiInfo apiInfo(){
		ApiInfo apiInfo = new ApiInfoBuilder()
				.title("中石化信息管理系统接口API")
				.description("中石化信息管理系统api")
				.termsOfServiceUrl("127.0.0.1:8888/Sinopec_infomation_system/api-docs")
				.version(version)
				.build();
		return apiInfo;
	}
	
}
3)在spring-mvc.xml中增加swaggerConfig的bean,让它交给spring进行管理。

 
    
 
    
 
 
 
    
 
    
<

<

<

<


注意:com.yusys.swagger.SwaggerConfig为我本地类的包路径,具体放在什么地方自己配置。
其中swagger-ui.html对应的location为classpath:/是因为生成的注解文件会放在根目录下。
4)修改web.xml

 
        springmvc
 
        org.springframework.web.servlet.DispatcherServlet
 
        
 
            contextConfigLocation
 
            classpath:spring-mvc.xml
 
        
 
        1
<

 
     springmvc
 
     /
<

这个地方必须配置,如果你配置的是*.XXX的形式会出现api-docs访问出错,这就会导致swagger-ui找不到api的有效路径。使swagger无法正常工作。

5)下载swagger-ui的文件,官网https://swagger.io/tools/swagger-ui/
下载完成后解压出来,把dist文件夹下面的所有文件复制到你当前项目的webContent下面。

6)修改index.html
把js部分的url修改成url: "127.0.0.1:8888/xxxx/v2/api-docs",其中xxxx为你当前项目名。

7)在controller层增加swagger的注解,此处只是一个例子。具体api可以查看swagger的文档
package com.isoftstone.api.customerDept.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.isoftstone.api.common.log.target.SystemControllerLog;
import com.isoftstone.api.common.utils.web.JsonResult;
import com.isoftstone.api.common.utils.web.StatusUtil;
import com.isoftstone.api.customerDept.entity.CustomerDept;
import com.isoftstone.api.customerDept.service.CustomerDeptService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("customerDept")
@Api(value="客户部门管理",tags="客户部门管理模块接口Api")
public class CustomerDeptController {

	@Autowired
	private CustomerDeptService	customerDeptService;
	
	@ApiOperation(value="客户部门信息创建",notes="客户部门信息创建")
	@SystemControllerLog(description="客户部门信息创建")
	@RequestMapping(value="/create",method=RequestMethod.POST)
	public JsonResult create(@RequestBody CustomerDept customerDept){
		return customerDeptService.create(customerDept);
	}
	
}

8)访问swagger-ui.html
SSM框架整合Swagger2(非maven项目者进)_第3张图片

你可能感兴趣的:(SSM框架整合Swagger2(非maven项目者进))