提交 e78d61a0 编写于 作者: Q qinyingjie

fix:添加swagger

上级 1f307777
......@@ -83,6 +83,32 @@
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!--集成redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!--集成swagger-->
<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>
</dependencies>
<build>
<plugins>
......
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();
}
}
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<String, Object> redisTemplate(RedisConnectionFactory factory) {
System.out.println("==================自定义的redisTemplate实例化了===================");
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
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;
}
}
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
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) {
......
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 数组分别存储
......
......@@ -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();
......
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);
}
}
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<User> 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);
......
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;
......
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;
}
......@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kwan.springbootkwan.entity.User;
import org.apache.ibatis.annotations.Mapper;
/**
* mapper文件
*
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册