提交 600d9dd9 编写于 作者: Q qinxiaodong@pannk.com

集成swagger

上级 60356d13
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-demo</artifactId>
<groupId>com.pannk</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>integrate-swagger</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<!--在引用时请在maven中央仓库搜索2.X最新版本号-->
<version>2.0.8</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.pannk;
import com.pannk.sys.user.entity.SysUserEntity;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import java.util.ArrayList;
import java.util.List;
/**
* Created by wolf on 20-11-30.
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Bean
public List<SysUserEntity> userList() {
List<SysUserEntity> list = new ArrayList<>();
SysUserEntity sysUserEntity = new SysUserEntity();
sysUserEntity.setId(1L);
sysUserEntity.setName("admin");
sysUserEntity.setCode("000001");
sysUserEntity.setAge(26);
sysUserEntity.setPassword("admin");
list.add(sysUserEntity);
sysUserEntity = new SysUserEntity();
sysUserEntity.setId(2L);
sysUserEntity.setName("lisi");
sysUserEntity.setCode("000002");
sysUserEntity.setAge(28);
sysUserEntity.setPassword("123456");
list.add(sysUserEntity);
return list;
}
}
package com.pannk.config;
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
/**
* Created by wolf on 20-11-30.
*/
@EnableSwagger2WebMvc
@Configuration
public class Swagger2Config {
/**
* 用于配置swagger2,包含文档基本信息
* 指定swagger2的作用域(这里指定包路径下的所有API)
*
* @return Docket
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.pannk"))
.paths(PathSelectors.any())
.build();
}
/**
* 构建文档基本信息,用于页面显示,可以包含版本、
* 联系人信息、服务地址、文档描述信息等
*
* @return ApiInfo
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//标题
.title("Spring Boot2中采用Swagger2构建RESTful APIs")
.description("通过访问swagger-ui.html,实现接口测试、文档生成")
.termsOfServiceUrl("http://localhost:8080")
//设置联系方式
.contact(new Contact("Dike", "http://www.pannk.com", "dikeqin@gmail.com"))
.version("1.0")
.build();
}
}
package com.pannk.sys.user.controller;
import com.pannk.sys.user.entity.SysUserEntity;
import com.pannk.sys.user.service.SysUserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
/**
* Created by wolf on 20-11-30.
*/
@Api(tags = "用户操作", description = "用户控制器")
@RestController
public class UserController {
@Autowired
private SysUserService sysUserService;
/**
* 查询用户列表
*
* @param filter 条件
* @return
*/
@ApiOperation(value = "查询用户列表", notes = "过滤条件:用户名称/编号", nickname = "list")
@ApiParam(name = "filter", value = "用户名/编号", required = true)
@ApiImplicitParams(@ApiImplicitParam(value = "过滤条件", name = "filter", required = true, paramType = "query"))
@ApiResponses(value = {@ApiResponse(code = 200, message = "查询成功", response = SysUserEntity.class,
responseContainer = "List")})
@GetMapping("/list")
public Map<String, Object> list(String filter) {
Map<String, Object> result = new HashMap<>();
result.put("code", 200);
result.put("message", "查询成功");
result.put("data", sysUserService.list(filter));
return result;
}
/**
* 查询详情
*
* @param id id
* @return
*/
@GetMapping("/detail/{id}")
public SysUserEntity detail(@PathVariable Long id) {
return sysUserService.detail(id);
}
/**
* 新则用户信息
*
* @param sysUserEntity sysUserEntity
* @return
*/
@PostMapping("/save")
public SysUserEntity save(@RequestBody SysUserEntity sysUserEntity) {
return sysUserService.save(sysUserEntity);
}
/**
* 更新用户信息
*
* @param sysUserEntity sysUserEntity
* @return
*/
@PostMapping("/update")
public SysUserEntity update(@RequestBody SysUserEntity sysUserEntity) {
return sysUserService.save(sysUserEntity);
}
/**
* 新则用户信息
*
* @param id id
* @return
*/
@PostMapping("/delete")
public String delete(Long id) {
sysUserService.delete(id);
return "OK";
}
/**
* 新则用户信息
*
* @param ids ids
* @return
*/
@PostMapping("/batchDelete")
public String batchDelete(Long[] ids) {
sysUserService.batchDelete(ids);
return "OK";
}
}
package com.pannk.sys.user.entity;
import lombok.Data;
/**
* Created by wolf on 20-11-30.
*/
@Data
public class SysUserEntity {
private Long id;
private String name;
private String code;
private String password;
private Integer age;
}
package com.pannk.sys.user.service;
import com.pannk.sys.user.entity.SysUserEntity;
import java.util.List;
import java.util.Map;
/**
* Created by wolf on 20-11-30.
*/
public interface SysUserService {
/**
* 查询用户列表
*
* @param filter 条件
* @return
*/
List<SysUserEntity> list(String filter);
/**
* 详情
*
* @param id id
* @return
*/
SysUserEntity detail(Long id);
/**
* 删除用户
*
* @param id id
*/
void delete(Long id);
/**
* 批量删除
*
* @param ids ids
*/
void batchDelete(Long[] ids);
/**
* 新增
*
* @param sysUserEntity 用户信息
* @return
*/
SysUserEntity save(SysUserEntity sysUserEntity);
/**
* 更新
*
* @param sysUserEntity
* @return
*/
SysUserEntity update(SysUserEntity sysUserEntity);
}
package com.pannk.sys.user.service.impl;
import com.pannk.sys.user.entity.SysUserEntity;
import com.pannk.sys.user.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by wolf on 20-11-30.
*/
@Service
public class SysUserServiceImpl implements SysUserService {
@Autowired
private List<SysUserEntity> list;
@Override
public List<SysUserEntity> list(String filter) {
List<SysUserEntity> result = new ArrayList<>();
list.forEach(u -> {
if (u.getCode().equalsIgnoreCase(filter) || u.getName().equalsIgnoreCase(filter)) {
result.add(u);
}
});
return result;
}
@Override
public SysUserEntity detail(Long id) {
for (SysUserEntity sysUserEntity : list) {
if (sysUserEntity.getId() == id) {
return sysUserEntity;
}
}
return null;
}
@Override
public void delete(Long id) {
for (SysUserEntity sysUserEntity : list) {
if (sysUserEntity.getId() == id) {
list.remove(sysUserEntity);
}
}
}
@Override
public void batchDelete(Long[] ids) {
for (SysUserEntity sysUserEntity : list) {
if (Arrays.asList(ids).contains(sysUserEntity.getId())) {
list.remove(sysUserEntity);
}
}
}
@Override
public SysUserEntity save(SysUserEntity sysUserEntity) {
list.add(sysUserEntity);
return sysUserEntity;
}
@Override
public SysUserEntity update(SysUserEntity sysUserEntity) {
for (int i = 0; i < list.size(); i++) {
SysUserEntity u = list.get(i);
if (u.getId() == sysUserEntity.getId()) {
list.remove(u);
list.add(i, sysUserEntity);
}
}
return sysUserEntity;
}
}
......@@ -19,6 +19,7 @@
<module>integrate-redis</module>
<module>integrate-mongodb</module>
<module>integrate-mq</module>
<module>integrate-swagger</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册