Skip to content

Commit e1cfb12

Browse files
author
quding
committed
add swagger
1 parent 21314b1 commit e1cfb12

File tree

6 files changed

+307
-0
lines changed

6 files changed

+307
-0
lines changed

Spring-Data-Redis/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,27 @@
3030
<groupId>org.springframework.boot</groupId>
3131
<artifactId>spring-boot-starter-data-redis</artifactId>
3232
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-web</artifactId>
36+
</dependency>
3337
<dependency>
3438
<groupId>org.springframework.boot</groupId>
3539
<artifactId>spring-boot-starter-test</artifactId>
3640
<scope>test</scope>
3741
</dependency>
42+
<!--swagger start-->
43+
<dependency>
44+
<groupId>io.springfox</groupId>
45+
<artifactId>springfox-swagger-ui</artifactId>
46+
<version>2.6.1</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>io.springfox</groupId>
50+
<artifactId>springfox-swagger2</artifactId>
51+
<version>2.6.1</version>
52+
</dependency>
53+
3854
</dependencies>
3955

4056
<build>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package cn.mrdear.conf;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.context.request.async.DeferredResult;
6+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
7+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
8+
9+
import springfox.documentation.builders.ApiInfoBuilder;
10+
import springfox.documentation.service.ApiInfo;
11+
import springfox.documentation.service.Contact;
12+
import springfox.documentation.spi.DocumentationType;
13+
import springfox.documentation.spring.web.plugins.Docket;
14+
import springfox.documentation.swagger2.annotations.EnableSwagger2;
15+
16+
/**
17+
* @author Niu Li
18+
* @since 2017/3/30
19+
*/
20+
@Configuration
21+
@EnableSwagger2
22+
public class SwaggerConfig extends WebMvcConfigurerAdapter {
23+
24+
/**
25+
* 使用enableMVC注解的话,该配置必须,否则无法映射资源
26+
*/
27+
@Override
28+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
29+
registry.addResourceHandler("swagger-ui.html")
30+
.addResourceLocations("classpath:/META-INF/resources/");
31+
registry.addResourceHandler("/webjars/**")
32+
.addResourceLocations("classpath:/META-INF/resources/webjars/");
33+
34+
}
35+
36+
@Bean
37+
public Docket createRestApi() {
38+
return new Docket(DocumentationType.SWAGGER_2)
39+
.groupName("api")
40+
.genericModelSubstitutes(DeferredResult.class)
41+
.useDefaultResponseMessages(false)
42+
.forCodeGeneration(true)
43+
.pathMapping("/")
44+
.select()
45+
.build()
46+
.apiInfo(apiInfo());
47+
}
48+
49+
private ApiInfo apiInfo() {
50+
Contact contact = new Contact("屈定","http://mrdear.cn","niudear@foxmail.com");
51+
return new ApiInfoBuilder()
52+
.title("Spring Data Redis")
53+
.description("Spring Data Redis学习记录")
54+
.termsOfServiceUrl("http://mrdear.cn")
55+
.contact(contact)
56+
.version("1.0")
57+
.build();
58+
}
59+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package cn.mrdear.controller;
2+
3+
import org.springframework.web.bind.annotation.PathVariable;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RequestMethod;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.Objects;
11+
12+
import javax.annotation.Resource;
13+
14+
import cn.mrdear.dao.UserDao;
15+
import cn.mrdear.model.User;
16+
import cn.mrdear.util.MockUserUtil;
17+
import io.swagger.annotations.Api;
18+
import io.swagger.annotations.ApiOperation;
19+
import io.swagger.annotations.ApiResponse;
20+
21+
/**
22+
* @author Niu Li
23+
* @since 2017/3/30
24+
*/
25+
@Api(description = "提供增删改查,swagger示例",tags = "API-UserController")
26+
@RestController
27+
@RequestMapping(value = "/api/v1")
28+
public class UserController {
29+
@Resource
30+
private UserDao userDao;
31+
32+
@ApiOperation(value = "查询全部用户",httpMethod = "GET")
33+
@ApiResponse(code = 200,message = "Success")
34+
@RequestMapping(value = "/users",method = RequestMethod.GET)
35+
public List<User> getAllUsers(){
36+
return userDao.query();
37+
}
38+
39+
@ApiOperation(value = "查询单个用户",notes = "根据传入id查找用户",httpMethod = "GET")
40+
@ApiResponse(code = 200,message = "Success")
41+
@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
42+
public User getUser(@PathVariable("id") Integer id){
43+
return userDao.find(id);
44+
}
45+
46+
@ApiOperation(value = "批量插入用户",notes = "根据传入参数增加用户",httpMethod = "POST")
47+
@ApiResponse(code = 200,message = "Success")
48+
@RequestMapping(value = "/user",method = RequestMethod.POST)
49+
public List<User> InsertUser(Integer nums){
50+
if (Objects.isNull(nums)){
51+
nums = 10;
52+
}
53+
List<User> users = new ArrayList<>();
54+
for (Integer i = 0; i < nums; i++) {
55+
User user = MockUserUtil.mockSignle();
56+
userDao.save(user);
57+
users.add(user);
58+
}
59+
return users;
60+
}
61+
62+
63+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package cn.mrdear.dao;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.data.redis.core.HashOperations;
9+
import org.springframework.data.redis.core.StringRedisTemplate;
10+
import org.springframework.stereotype.Repository;
11+
12+
import java.io.IOException;
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
import java.util.Objects;
16+
17+
import javax.annotation.Resource;
18+
19+
import cn.mrdear.model.User;
20+
21+
/**
22+
* redis操作示例
23+
*
24+
* @author Niu Li
25+
* @since 2017/3/30
26+
*/
27+
@Repository
28+
public class UserDao {
29+
private static Logger logger = LoggerFactory.getLogger(UserDao.class);
30+
@Resource
31+
private StringRedisTemplate stringRedisTemplate;
32+
33+
private static final String KEY_PREFIX = "users";
34+
35+
private ObjectMapper objectMapper = new ObjectMapper();
36+
37+
/**
38+
* 保存一个用户
39+
*/
40+
public void save(User user) {
41+
HashOperations<String, String, String> opsForHash = stringRedisTemplate.opsForHash();
42+
try {
43+
opsForHash.put(KEY_PREFIX, user.getId() + "", objectMapper.writeValueAsString(user));
44+
} catch (JsonProcessingException e) {
45+
logger.error("save user error,user id is ", user.getId());
46+
}
47+
}
48+
49+
/**
50+
* 插入一个用户
51+
* @param id 用户id
52+
* @return 用户
53+
*/
54+
public User find(Integer id) {
55+
try {
56+
HashOperations<String, String, String> opsForHash = stringRedisTemplate.opsForHash();
57+
String userJson = opsForHash.get(KEY_PREFIX, id.toString());
58+
if (Objects.isNull(userJson)){
59+
return null;
60+
}
61+
return objectMapper.readValue(userJson, User.class);
62+
} catch (IOException e) {
63+
logger.error("find user error id is {}",id);
64+
}
65+
return null;
66+
}
67+
68+
/**
69+
* 查询全部用户
70+
*/
71+
public List<User> query(){
72+
HashOperations<String, String, String> opsForHash = stringRedisTemplate.opsForHash();
73+
List<String> usersJson = opsForHash.values(KEY_PREFIX);
74+
List<User> users = new ArrayList<>(usersJson.size());
75+
usersJson.forEach(x -> {
76+
try {
77+
users.add(objectMapper.readValue(x,User.class));
78+
} catch (IOException e) {
79+
logger.error("read user error id is {}",x);
80+
}
81+
});
82+
return users;
83+
}
84+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cn.mrdear.model;
2+
3+
/**
4+
* 测试用户
5+
* @author Niu Li
6+
* @since 2017/3/30
7+
*/
8+
public class User {
9+
private Long id;
10+
private String username;
11+
private String password;
12+
13+
public Long getId() {
14+
return id;
15+
}
16+
17+
public void setId(Long id) {
18+
this.id = id;
19+
}
20+
21+
public String getUsername() {
22+
return username;
23+
}
24+
25+
public void setUsername(String username) {
26+
this.username = username;
27+
}
28+
29+
public String getPassword() {
30+
return password;
31+
}
32+
33+
public void setPassword(String password) {
34+
this.password = password;
35+
}
36+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cn.mrdear.util;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Random;
6+
import java.util.UUID;
7+
8+
import cn.mrdear.model.User;
9+
10+
/**
11+
* mock虚拟用户
12+
* @author Niu Li
13+
* @since 2017/3/30
14+
*/
15+
public class MockUserUtil {
16+
17+
//mock数据来源
18+
private static String[] namePrefix = {"赵","钱","孙","李","周","吴","郑","王"};
19+
private static String[] nameSuffix = {"一","二","三","四","五","六","七","八"};
20+
21+
private static Random random = new Random();
22+
23+
/**
24+
* mock用户
25+
* @return 用户实体
26+
*/
27+
public static User mockSignle(){
28+
User user = new User();
29+
int x = random.nextInt(namePrefix.length);
30+
String userName = namePrefix[x]+nameSuffix[x];
31+
user.setId((long) x);
32+
user.setUsername(userName);
33+
user.setPassword(UUID.randomUUID().toString());
34+
return user;
35+
}
36+
37+
/**
38+
* mock集合
39+
* @param num 集合数量
40+
* @return 集合
41+
*/
42+
public static List<User> mockList(int num){
43+
List<User> users = new ArrayList<>();
44+
for (int i = 0; i < num; i++) {
45+
users.add(mockSignle());
46+
}
47+
return users;
48+
}
49+
}

0 commit comments

Comments
 (0)