From e78d61a03d4f5d3b71494533cb7900a94af8068a Mon Sep 17 00:00:00 2001 From: qinyingjie Date: Mon, 19 Dec 2022 17:46:02 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E6=B7=BB=E5=8A=A0swagger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 26 +++++++++++ .../config/DataSourceConfig.java | 24 ---------- .../springbootkwan/config/RedisConfig.java | 40 ++++++++++++++++ .../springbootkwan/config/Swagger2Config.java | 46 +++++++++++++++++++ .../controller/BookController.java | 6 ++- .../controller/FileUploadController.java | 5 ++ .../controller/PersonController.java | 4 ++ .../controller/RedisController.java | 44 ++++++++++++++++++ .../controller/UserController.java | 7 ++- .../kwan/springbootkwan/entity/Person.java | 7 ++- .../com/kwan/springbootkwan/entity/User.java | 11 ++++- .../springbootkwan/mapper/UserMapper.java | 1 + src/main/resources/application.yaml | 11 +++++ 13 files changed, 203 insertions(+), 29 deletions(-) delete mode 100644 src/main/java/com/kwan/springbootkwan/config/DataSourceConfig.java create mode 100644 src/main/java/com/kwan/springbootkwan/config/RedisConfig.java create mode 100644 src/main/java/com/kwan/springbootkwan/config/Swagger2Config.java create mode 100644 src/main/java/com/kwan/springbootkwan/controller/RedisController.java diff --git a/pom.xml b/pom.xml index 5b0293a..7e28bdb 100644 --- a/pom.xml +++ b/pom.xml @@ -83,6 +83,32 @@ druid-spring-boot-starter 1.1.10 + + + org.springframework.boot + spring-boot-starter-data-redis + + + io.lettuce + lettuce-core + + + + + redis.clients + jedis + + + + io.springfox + springfox-swagger2 + 2.6.1 + + + io.springfox + springfox-swagger-ui + 2.6.1 + diff --git a/src/main/java/com/kwan/springbootkwan/config/DataSourceConfig.java b/src/main/java/com/kwan/springbootkwan/config/DataSourceConfig.java deleted file mode 100644 index bd4a6e7..0000000 --- a/src/main/java/com/kwan/springbootkwan/config/DataSourceConfig.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.kwan.springbootkwan.config; - -import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import javax.sql.DataSource; - -@Configuration -public class DataSourceConfig { - - @Bean - @ConfigurationProperties("spring.datasource.one") - DataSource dataSourceOne() { - return DruidDataSourceBuilder.create().build(); - } - - @Bean - @ConfigurationProperties("spring.datasource.two") - DataSource dataSourceTwo() { - return DruidDataSourceBuilder.create().build(); - } -} diff --git a/src/main/java/com/kwan/springbootkwan/config/RedisConfig.java b/src/main/java/com/kwan/springbootkwan/config/RedisConfig.java new file mode 100644 index 0000000..1c18b95 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/config/RedisConfig.java @@ -0,0 +1,40 @@ +package com.kwan.springbootkwan.config; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +@Configuration +public class RedisConfig { + + @Bean + @SuppressWarnings("all") + public RedisTemplate redisTemplate(RedisConnectionFactory factory) { + System.out.println("==================自定义的redisTemplate实例化了==================="); + RedisTemplate template = new RedisTemplate(); + template.setConnectionFactory(factory); + Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); + ObjectMapper om = new ObjectMapper(); + om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); + om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); + jackson2JsonRedisSerializer.setObjectMapper(om); + StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); + // key采用String的序列化方式 + template.setKeySerializer(stringRedisSerializer); + // hash的key也采用String的序列化方式 + template.setHashKeySerializer(stringRedisSerializer); + // value序列化方式采用jackson + template.setValueSerializer(jackson2JsonRedisSerializer); + // hash的value序列化方式采用jackson + template.setHashValueSerializer(jackson2JsonRedisSerializer); + template.afterPropertiesSet(); + return template; + } +} + diff --git a/src/main/java/com/kwan/springbootkwan/config/Swagger2Config.java b/src/main/java/com/kwan/springbootkwan/config/Swagger2Config.java new file mode 100644 index 0000000..aab803d --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/config/Swagger2Config.java @@ -0,0 +1,46 @@ +package com.kwan.springbootkwan.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.EnableSwagger2; + +/** + * 通过@Configuration注解,表明它是一个配置类 @EnableSwagger2开启swagger2。 apiINfo()配置一些基本的信息。apis()指定扫描的包会生成文档。 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/12/19 17:23 + */ +@Configuration +@EnableSwagger2 +public class Swagger2Config { + + @Bean + public Docket createRestApi() { + return new Docket(DocumentationType.SWAGGER_2) + .groupName("SpringBoot学习") + .apiInfo(apiInfo()) + .select() + .apis(RequestHandlerSelectors.basePackage("com.kwan.springbootkwan.controller"))//需要生成接口文档的包名 + .paths(PathSelectors.any()) + .build(); + } + + private ApiInfo apiInfo() { + return new ApiInfoBuilder() + .title("SpringBoot学习") + .description("最怕一生碌碌无为,还安慰自己平凡可贵。") + .termsOfServiceUrl("http://qinyingjie.top/") + .contact(new Contact("kwan", "http://qinyingjie.top/", "3327782001@qq.com")) + .version("Apache 2.0") + .build(); + } + +} \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/controller/BookController.java b/src/main/java/com/kwan/springbootkwan/controller/BookController.java index ab087d2..89e0298 100644 --- a/src/main/java/com/kwan/springbootkwan/controller/BookController.java +++ b/src/main/java/com/kwan/springbootkwan/controller/BookController.java @@ -1,5 +1,7 @@ package com.kwan.springbootkwan.controller; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.*; /** @@ -9,16 +11,18 @@ import org.springframework.web.bind.annotation.*; * @version : 2.2.0 * @date : 2022/12/19 16:10 */ +@Api(description = "书籍信息", tags = "BookController") @RestController public class BookController { + @ApiOperation(value = "添加书籍", notes = "添加书籍") @CrossOrigin(value = "http://localhost:8081", maxAge = 1800, allowedHeaders = "*") @PostMapping("addBook") public String addBook(String name) { return "receive:" + name; } - + @ApiOperation(value = "删除书籍", notes = "删除书籍") @CrossOrigin(value = "http://localhost:8081", maxAge = 1800, allowedHeaders = "*") @DeleteMapping("deleteBook/{id}") public String deleteBook(@PathVariable(value = "id") String id) { diff --git a/src/main/java/com/kwan/springbootkwan/controller/FileUploadController.java b/src/main/java/com/kwan/springbootkwan/controller/FileUploadController.java index 5f1a1c2..df2b520 100644 --- a/src/main/java/com/kwan/springbootkwan/controller/FileUploadController.java +++ b/src/main/java/com/kwan/springbootkwan/controller/FileUploadController.java @@ -1,5 +1,7 @@ package com.kwan.springbootkwan.controller; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RestController; @@ -19,6 +21,7 @@ import java.util.UUID; * @version : 2.2.0 * @date : 2022/12/19 16:07 */ +@Api(description = "文件上传信息", tags = "FileUploadController") @RestController public class FileUploadController { @@ -29,6 +32,7 @@ public class FileUploadController { * @param req * @return */ + @ApiOperation(value = "单个文件上传", notes = "单个文件上传") @PostMapping("/upload") public String upload(MultipartFile uploadFile, HttpServletRequest req) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); @@ -62,6 +66,7 @@ public class FileUploadController { * @param req * @return */ + @ApiOperation(value = "批量上传", notes = "批量上传") @PostMapping("/uploadBatch") public String uploadBatch(@RequestPart("uploadFile") MultipartFile[] uploadFiles, HttpServletRequest req) { //遍历 uploadFiles 数组分别存储 diff --git a/src/main/java/com/kwan/springbootkwan/controller/PersonController.java b/src/main/java/com/kwan/springbootkwan/controller/PersonController.java index 9b60162..92ee4b7 100644 --- a/src/main/java/com/kwan/springbootkwan/controller/PersonController.java +++ b/src/main/java/com/kwan/springbootkwan/controller/PersonController.java @@ -3,6 +3,8 @@ package com.kwan.springbootkwan.controller; import com.kwan.springbootkwan.entity.Person; import com.kwan.springbootkwan.entity.User; import com.kwan.springbootkwan.service.IUserService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -16,6 +18,7 @@ import java.util.List; * @version : 2.2.0 * @date : 2022/12/19 16:08 */ +@Api(description = "person用户信息", tags = "PersonController") @RestController @RequestMapping("/person") public class PersonController { @@ -26,6 +29,7 @@ public class PersonController { * "birthday": "2022-12-19" * } */ + @ApiOperation(value = "json返回", notes = "json返回") @GetMapping("/person") public Person person() { Person person = new Person(); diff --git a/src/main/java/com/kwan/springbootkwan/controller/RedisController.java b/src/main/java/com/kwan/springbootkwan/controller/RedisController.java new file mode 100644 index 0000000..c28a5c5 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/controller/RedisController.java @@ -0,0 +1,44 @@ +package com.kwan.springbootkwan.controller; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import java.util.concurrent.TimeUnit; + +@Api(description = "redis测试信息", tags = "RedisController") +@RestController +@RequestMapping("/test") +public class RedisController { + + public static final String REDIS_KEY = "test:_w"; + + @Resource + private RedisTemplate redisTemplate; + + + @ApiOperation(value = "redis添加数据", notes = "redis添加数据") + @GetMapping("/add") + public void add() { + //当前时间时间戳 + String nowStr = String.valueOf(System.currentTimeMillis()); + //设置redis中的key值 + String redisKey = REDIS_KEY.concat(nowStr); + //向对应的key值添加数据 + redisTemplate.opsForValue().set(REDIS_KEY, "测试向redis中添加数据"); + //设置过期时间 + redisTemplate.expire(REDIS_KEY, 2000000, TimeUnit.SECONDS); + } + + @ApiOperation(value = "redis获取数据", notes = "redis获取数据") + @GetMapping("/get") + public void get() { + //获取redis中的数据 + String str = (String) redisTemplate.opsForValue().get(REDIS_KEY); + System.out.println("获取redis中的数据-->" + str); + } +} diff --git a/src/main/java/com/kwan/springbootkwan/controller/UserController.java b/src/main/java/com/kwan/springbootkwan/controller/UserController.java index 4c5c08e..1d43e7e 100644 --- a/src/main/java/com/kwan/springbootkwan/controller/UserController.java +++ b/src/main/java/com/kwan/springbootkwan/controller/UserController.java @@ -1,12 +1,12 @@ package com.kwan.springbootkwan.controller; -import com.kwan.springbootkwan.entity.Person; import com.kwan.springbootkwan.entity.User; import com.kwan.springbootkwan.service.IUserService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import java.util.Date; import java.util.List; /** @@ -16,6 +16,7 @@ import java.util.List; * @version : 2.2.0 * @date : 2022/12/19 16:08 */ +@Api(description = "用户信息", tags = "UserController") @RestController @RequestMapping("/user") public class UserController { @@ -23,11 +24,13 @@ public class UserController { @Autowired private IUserService userService; + @ApiOperation(value = "获取所有用户", notes = "获取所有用户") @RequestMapping(value = "/all", method = RequestMethod.GET) public List addAdvertise() { return userService.getUsers(); } + @ApiOperation(value = "根据id获取用户信息", notes = "根据id获取用户信息") @RequestMapping(value = "/getUserById/{id}", method = RequestMethod.GET) public User getUserById(@PathVariable Integer id) { return userService.getUserById(id); diff --git a/src/main/java/com/kwan/springbootkwan/entity/Person.java b/src/main/java/com/kwan/springbootkwan/entity/Person.java index 6e1b399..a609748 100644 --- a/src/main/java/com/kwan/springbootkwan/entity/Person.java +++ b/src/main/java/com/kwan/springbootkwan/entity/Person.java @@ -1,13 +1,18 @@ package com.kwan.springbootkwan.entity; import com.fasterxml.jackson.annotation.JsonFormat; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; import java.util.Date; +@ApiModel(value = "用户基本信息") public class Person { - + @ApiModelProperty(value = "姓名") private String name; + @ApiModelProperty(value = "年龄") private Integer age; + @ApiModelProperty(value = "生日") @JsonFormat(pattern = "yyyy-MM-dd") private Date birthday; diff --git a/src/main/java/com/kwan/springbootkwan/entity/User.java b/src/main/java/com/kwan/springbootkwan/entity/User.java index 27738fa..20546ac 100644 --- a/src/main/java/com/kwan/springbootkwan/entity/User.java +++ b/src/main/java/com/kwan/springbootkwan/entity/User.java @@ -1,18 +1,27 @@ package com.kwan.springbootkwan.entity; + import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; import lombok.Builder; import lombok.Data; @Data @Builder @TableName(value = "user") -public class User { +@ApiModel(value = "用户基本信息") +public class User { + @ApiModelProperty(value = "主键id") @TableId(value = "id", type = IdType.AUTO) private Integer id; + @ApiModelProperty(value = "姓名") private String name; + @ApiModelProperty(value = "性别") private String sex; + @ApiModelProperty(value = "密码") private String pwd; + @ApiModelProperty(value = "邮箱") private String email; } diff --git a/src/main/java/com/kwan/springbootkwan/mapper/UserMapper.java b/src/main/java/com/kwan/springbootkwan/mapper/UserMapper.java index 19688a5..4f210e9 100644 --- a/src/main/java/com/kwan/springbootkwan/mapper/UserMapper.java +++ b/src/main/java/com/kwan/springbootkwan/mapper/UserMapper.java @@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.kwan.springbootkwan.entity.User; import org.apache.ibatis.annotations.Mapper; + /** * mapper文件 * diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 02e415c..b8c6909 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -6,6 +6,17 @@ swagger: #兼容swagger配置 spring: + redis: + database: 0 # Redis数据库索引(默认为0) + host: 120.79.36.53 #Redis服务器地址 + port: 6379 # Redis服务器连接端口 + password: # Redis服务器连接密码(默认为空) + jedis: + pool: + max-active: 200 # 连接池最大连接数(使用负值表示没有限制) + max-idle: 10 # 连接池中的最大空闲连接 + min-idle: 0 # 连接池中的最小空闲连接 + max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制) mvc: pathmatch: matching-strategy: ant_path_matcher -- GitLab