1.导入相关依赖
<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>
2.Swagger配置文件config
@Configuration//将config加入到springboot里
@EnableSwagger2//开启Swagger2(Swagger2是新版本)
public class Swagger2 {
}
3.测试
http://localhost:8080/swagger-ui.html
4.配置Swagger
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(this.apiInfo())
.select()//选择哪些路径和api会生成document
.apis(RequestHandlerSelectors.basePackage("com.synda.ynmzgx"))//对指定包进行监控
.paths(PathSelectors.any())//对所有路径进行监控
.build()
.globalOperationParameters(setHeaderToken());
}
//配置swagger信息=apiInfo
private ApiInfo apiInfo(){
//作者信息
Contact contact = new Contact( name: "xx",url: "https://blog.csdn.net/AllenLFC",email: "xxx@qq.com" );
return new ApiInfo(
title: "SwaggerAPI文档" ,
description:"即使再小的帆也能远航",
version: "v1.0" ,
termsOfServiceUrl: "https://blog.csdn.net/AllenLFC",
contact,
license: "Apache 2.0",
licenseUrl: "http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList()
);
}
5.使用相应注解
/**
* @program: jpademo
* @description: EmployeeController
* @author: ZengGuangfu
* @create 2018-10-23 11:07
*/
@RestController
@RequestMapping("emp")
@Api(value = "用户管理类")
public class EmployeeController {
@Autowired
private EmployeeReposiroty employeeReposiroty;
/**
* 增加人物
* @param employee
* @return
*/
@PostMapping(value = "employee")
@ApiOperation(value = "新增一个用户",notes = "新增之后返回对象")
@ApiImplicitParam(paramType = "query",name = "employee",value = "用户",required = true)
@ApiResponse(code = 400,message = "参数没有填好",response = String.class)
public String insert(Employee employee){
Employee employee1 = employeeReposiroty.save(employee);
if(employee1 != null) {
return SysNode.Judge.SUCCESS.getResult();
}else {
return SysNode.Judge.FAILD.getResult();
}
}
/**
* 删除单个用户
* @param id
* @return
*/
@DeleteMapping(value = "employee/{id}")
@ApiOperation(value = "删除用户",notes = "根据成员id删除单个用户")
@ApiImplicitParam(paramType = "path",name = "id",value = "用户id",required = true,dataType = "Integer")
@ApiResponse(code = 400,message = "参数没有填好",response = String.class)
public String delete(@PathVariable("id")Integer id){
try{
employeeReposiroty.deleteById(id);
return SysNode.Judge.SUCCESS.getResult();
}catch (Exception e){
e.printStackTrace();
return SysNode.Judge.FAILD.getResult();
}
}
/**
* 修改单个成员
* @param employee
* @return
*/
@PutMapping(value = "employee/{id}")
@ApiOperation(value = "修改用户信息",notes = "根据成员id修改单个用户")
public String update(Employee employee){
/**
* save方法如果参数属性缺失,会导致原本存在的数据为null
*/
Employee employee1 = employeeReposiroty.saveAndFlush(employee);
if (employee1 != null) {
return SysNode.Judge.SUCCESS.getResult();
}else {
return SysNode.Judge.FAILD.getResult();
}
}
/**
* 获取所有成员,升序排列
* @return
*/
@GetMapping(value = "employee/sort")
@ApiOperation(value = "查询全部用户",notes = "默认根据升序查询全部用户信息")
public List<Employee> findAll(){
Sort orders = new Sort(Sort.Direction.DESC,"employeeId");
List<Employee> employeeList = employeeReposiroty.findAll(orders);
return employeeList;
}
/**
* 获取所有成员,升序排列
* @return
*/
@GetMapping(value = "employee/pageSort")
@ApiOperation(value = "查询用户信息",notes = "查询用户信息")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query",name = "sort",value = "排序方式:asc|desc",dataType = "String",required = true),
@ApiImplicitParam(paramType = "query",name = "pagenumber",value = "第几页",dataType = "Integer",required = true),
@ApiImplicitParam(paramType = "query",name = "pageSize",value = "分页数",dataType = "Integer",required = true)
})
public List<Employee> findAllByPage(String sort,Integer pagenumber,Integer pageSize){
try {
Sort.Direction sortlast;
if("desc".equals(sort.toLowerCase())){
sortlast = Sort.Direction.DESC;
}else{
sortlast = Sort.Direction.ASC;
}
Sort orders = new Sort(sortlast, "employeeId");
Pageable pageable = new PageRequest(pagenumber, pageSize, orders);
Page<Employee> employeePage = employeeReposiroty.findAll(pageable);
List<Employee> employeeList = employeePage.getContent();
return employeeList;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
/**
* 自定义拓展jpa,根据用户名查找单个用户
* @param username
* @return
*/
@GetMapping(value = "employee/find/{username}")
@ApiOperation(value = "查询用户信息",notes = "根据用户登录名查询该用户信息")
@ApiImplicitParam(paramType = "path",name = "username",value = "用户登录名",required = true,dataType = "String")
public Employee findByUsername(@PathVariable("username") String username){
List<Employee> employeeList = employeeReposiroty.findByUserNameOrderByEmployeeIdAsc(username);
if (employeeList != null && !employeeList.isEmpty()){
return employeeList.get(0);
}
return null;
}
/**
* 测试用
* @return
*/
@GetMapping(value = "employee/grade")
public List<Object[]> findEmployeeAndGrade(){
Pageable pageable = new PageRequest(0,3);
Page<Object[]> page = employeeReposiroty.findEmployeeAndGrade(pageable);
System.out.println(page.getTotalElements()+"----------结果总数------------");
System.out.println(page.getTotalPages()+"--------根据pageSize的总页数-----------");
System.out.println(page.getNumber()+"--------当前页数,pageNumber----------");
System.out.println(page.getNumberOfElements()+"--------当前页有几个数据--------");
System.out.println(page.getSize()+"---------PageSize-------------");
System.out.println(page.getSort()+"---------排序方式,没有则是'UNSORTED'----------");
List<Object[]> objects = page.getContent();
return objects;
}
}
6.测试显示
localhost:8080/swagger-ui.html
上线前注意关闭Swagger,不然会降低效率