额,网上的集成方法挺多的,但是自己发现 集成的时候还是会遇到很多问题
具体的步骤如下:
1.加入jar (pom中一定要有 jackson 这些,而且版本要和spring的版本兼容,,不然 跑起来有错误,具体什么错我忘记了。。。我用的是spring 4.x)
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.0</version>
</dependency>
2.swagger配置类的编写
package com.swagger.test.config;
import com.google.common.base.Predicates;
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 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;
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages = {"com.swagger.test"})
public class SwaggerConfig {
@SuppressWarnings("deprecation")
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo("xxxx 描述文档",//大标题
"项目组",//小标题
"1.0",//版本
"",
"xxxx",//作者
"xxx",//链接显示文字
"http://www.xxx.com/"//网站链接
);
return apiInfo;
}
//不同的分组 创建不同的docket就可以了
@Bean
public Docket publicComponentApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("用户")
.genericModelSubstitutes(DeferredResult.class)
.useDefaultResponseMessages(false)
.forCodeGeneration(false)
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.basePackage("com.swagger.test.controller"))
.paths(Predicates.or(PathSelectors.any()))//过滤的接口
.build()
.apiInfo(apiInfo());
}
@Bean
public Docket aa() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("流程")
.genericModelSubstitutes(DeferredResult.class)
.useDefaultResponseMessages(false)
.forCodeGeneration(false)
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.basePackage("com.swagger.test.controller"))
.paths(Predicates.or(PathSelectors.any()))//过滤的接口
.build()
.apiInfo(apiInfo());
}
}
3.spring-mvc配置文件中加入配置
<!-- swagger -->
<bean class="com.swagger.test.config.SwaggerConfig"></bean>
<mvc:default-servlet-handler/>
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
4.可以开始使用了,,,
package com.swagger.test.controller;
import com.swagger.test.entity.User;
import io.swagger.annotations.*;
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 java.util.List;
/**
* @author lf
* @Title: TestController
* @Description: TODO
* @date 2018/8/16 16:30
*/
@Api(value = "user",description = "用户相关接口",hidden = false)
@RestController
@RequestMapping("user")
public class UserController {
@ApiOperation(value = "保存用户",httpMethod = "POST")
@RequestMapping(value = "save", method = RequestMethod.POST)
public User save(@RequestBody User user) {
User res = user;
return user;
}
@ApiOperation(value = "查找用户",httpMethod = "GET")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query",name = "userId",value = "用户id",dataType = "String"
,defaultValue = "123456",required = true),
})
@RequestMapping(value = "find", method = RequestMethod.GET)
public User find(String userId) {
User res = new User();
return res;
}
@ApiOperation(value = "删除用户",httpMethod = "POST")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query",name = "name",value = "用户名称",dataType = "String"
,defaultValue = "123456",required = true),
@ApiImplicitParam(paramType = "query",name = "age",value = "用户年龄",dataType = "String"
,defaultValue = "123456",required = true)
})
@RequestMapping(value = "delete",method = RequestMethod.POST)
public String delete(@RequestBody List<User> users){
return "OK";
}
}