依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
config配置
/**
1. swagger配置类
*/
@Configuration
@EnableSwagger2
//@PropertySource("classpath:/application.properties")
public class SwaggerConfig {
@Value("${server.port}")
private String Ip;
@Value("${server.swagger.bao}")
private String bao;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//是否开启 (true 开启 false隐藏。生产环境建议隐藏)
//.enable(false)
.select()
//扫描的路径包,设置basePackage会将包下的所有被@Api标记类的所有方法作为api
.apis(RequestHandlerSelectors.basePackage(bao))
//指定路径处理PathSelectors.any()代表所有的路径
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//设置文档标题(API名称)
.title("SpringBoot中使用Swagger2接口规范")
//文档描述
.description("接口说明")
//服务条款URL
.termsOfServiceUrl("http://localhost:"+Ip)
//版本号
.version("1.0.0")
.build();
}
}
控制层
package com.cy.pj.activity.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cy.pj.activity.dao.UserInfoDao;
import com.cy.pj.activity.pojo.UserInfoEntity;
import com.cy.pj.activity.service.UserInfoService;
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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collection;
import java.util.List;
/**
* @Description UserInfoController
* @Author Sans
* @CreateTime 2019/6/8 16:27
*/
@RestController
@RequestMapping("/userInfo")
@Api(value = "swagger测试接口value", tags = "swagger接口tags", description = "swagger测试接口description")
public class UserInfoController {
@Autowired
private UserInfoService userInfoService;
@Autowired
private UserInfoDao userInfoDao;
@GetMapping (value = "/getList2")
//方法参数说明,name参数名;value参数说明,备注;dataType参数类型;required 是否必传;defaultValue 默认值
@ApiImplicitParam(name = "UserInfoEntity", value = "信息实体类")
//说明是什么方法(可以理解为方法注释)
@ApiOperation(value = "mybatis-plus查询所有数据value", notes = "查询信息notes")
public Collection<UserInfoEntity> getList2(){
// QueryWrapper<UserInfoEntity> queryWrapper1 = new QueryWrapper<>();
List<UserInfoEntity> userInfoEntityList = userInfoDao.selectList(null);
return userInfoEntityList;
}
@GetMapping("/save1")
public UserInfoEntity InsertUserInfoEntity(){
UserInfoEntity userInfoEntity = new UserInfoEntity();
userInfoEntity.setName("测试20210716").setPosition("测试位置").setDegree("1").setFrequency("2");
int count = userInfoDao.insert(userInfoEntity);
return userInfoEntity;
}
@GetMapping("/delete1/{id}")
@ApiImplicitParam(name = "id", value = "编号")
public UserInfoEntity deleteUserInfoEntity(@PathVariable("id") String id){
QueryWrapper<UserInfoEntity> queryWrapper1 = new QueryWrapper<>();
UserInfoEntity userInfoEntity = new UserInfoEntity();
UserInfoEntity userInfoEntity1 = userInfoDao.selectById(id);
userInfoDao.deleteById(id);
return userInfoEntity1;
}
@GetMapping("/getList3")
@ApiOperation(value = "正常sql查询信息value", notes = "正常sql查询信息notes")
public Collection<UserInfoEntity> getList3(){
// QueryWrapper<UserInfoEntity> queryWrapper1 = new QueryWrapper<>();
List<UserInfoEntity> userInfoEntities = userInfoDao.selectList();
return userInfoEntities;
}
}
swagger测试展示页面