<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
package com.shuju.test2.util.configuration;
import static springfox.documentation.builders.PathSelectors.regex;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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;
/**
* SwaggerConfig
*/
@Configuration
@EnableSwagger2
public class Swagger2Configuration {
/**
*
* @return
*/
@Bean
public Docket accessToken() {
return new Docket(DocumentationType.SWAGGER_2).groupName("api")// 定义组
.select() // 选择那些路径和api会生成document
.apis(RequestHandlerSelectors.basePackage("com.shuju.test2.controller")) // 拦截的包路径
.paths(regex("/api/.*"))// 拦截的接口路径
.build() // 创建
.apiInfo(apiInfo()); // 配置说明
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()//
.title("Demo")// 标题
.description("Api 項目")// 描述
.termsOfServiceUrl("http://www.shujuwajue.com")//
.contact(new Contact("revin", "http://www.shujuwajue.com", "509129@qq.com"))// 联系
//.license("Apache License Version 2.0")// 开源协议
//.licenseUrl("https://github.com/springfox/springfox/blob/master/LICENSE")// 地址
.version("1.0")// 版本
.build();
}
}
package com.shuju.test2.controller;
import java.util.Date;
import java.util.HashMap;
import com.shuju.test2.bean.DemoUserLog;
import com.shuju.test2.cache.DemoUserLogCache;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
@RestController
@RequestMapping("/api")
public class ApiController {
@Autowired
private DemoUserLogCache demoUserLogCache;
@RequestMapping(value = "/select", method = RequestMethod.GET)
@ApiOperation(value = "查找", notes = "根据用户ID查找用户")
public DemoUserLog get(@RequestParam(defaultValue = "1") Integer id) {
return demoUserLogCache.selectById(id);
}
@RequestMapping(value = "/update", method = RequestMethod.GET)
public DemoUserLog update(@RequestParam(defaultValue = "1") Integer id) {
DemoUserLog bean = demoUserLogCache.selectById(id);
bean.setUserName("测试222");
bean.setCreateTime(new Date());
demoUserLogCache.updateById(bean);
return bean;
}
@ApiIgnore
@RequestMapping(value = "/del", method = RequestMethod.GET)
public String del(@RequestParam(defaultValue = "1") Integer id) {
return demoUserLogCache.deleteById(id);
}
}