引入依赖
<!-- swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
创建swagger配置类
package com.example.demo.util;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
@Configuration
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot利用swagger构建api文档")
.description("简单优雅的restfun风格,http://blog.csdn.net/saytime")
.termsOfServiceUrl("http://blog.csdn.net/saytime")
.version("1.0")
.build();
}
}
在主类上上添加注解
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableScheduling
@EnableSwagger2
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Autowired
private RedisTemplate redisTemplate;
}
在接口上添加注解Api、ApiOperation
package com.example.demo.controller;
import com.example.demo.Service.UserService;
import com.example.demo.model.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @program: demo
* @description:
* @author: ht
* @create: 2019-07-16 20:02
*/
@Api(value = "用户管理")
@RestController
public class SysUserController {
@Autowired
private UserService userService;
@ApiOperation(value="用户登录", notes="用户登录")
@RequestMapping("/login")
public String login(){
SysUser sysUser = new SysUser();
sysUser.setPhone("----");
sysUser.setUsername("redis");
sysUser.setPhone("");
return SysUser.LOGIN_SUCCESS;
}
@ApiOperation(value="保存用户详细信息", notes="根据url的name来保存用户详细信息")
@ApiImplicitParam(name = "name", value = "用户名称", required = true, dataType = "String", paramType = "path")
@RequestMapping("/save/{name}")
public SysUser save(@PathVariable(value = "name")String name){
return userService.save(name,"123");
}
@ApiOperation(value="查询用户详细信息", notes="根据url的name来查询用户详细信息")
@ApiImplicitParam(name = "name", value = "用户名称", required = true, dataType = "String", paramType = "path")
@RequestMapping("/find/{name}")
public SysUser find(@PathVariable(value = "name")String name){
return userService.findUser(name);
}
@ApiOperation(value="删除用户详细信息", notes="根据url的name来删除用户详细信息")
@ApiImplicitParam(name = "name", value = "用户名称", required = true, dataType = "String", paramType = "path")
@RequestMapping("/remove/{name}")
public String remove(@PathVariable(value = "name")String name){
userService.removeUser(name);
return "SUCCESS";
}
}
启动项目时,会自动生产接口文档