Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8326642

Browse files
add swagger2 examples
1 parent ac31c08 commit 8326642

File tree

14 files changed

+685
-0
lines changed

14 files changed

+685
-0
lines changed

‎spring-boot-swagger/pom.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<artifactId>spring-boot-swagger</artifactId>
6+
<name>Spring Boot swagger Sample</name>
7+
<description>Spring Boot swagger Sample</description>
8+
9+
<parent>
10+
<groupId>org.springframework.boot</groupId>
11+
<artifactId>spring-boot-starter-parent</artifactId>
12+
<version>2.1.0.RELEASE</version>
13+
<relativePath/> <!-- lookup parent from repository -->
14+
</parent>
15+
16+
<dependencies>
17+
<!-- Compile -->
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-web</artifactId>
21+
</dependency>
22+
<dependency>
23+
<groupId>io.springfox</groupId>
24+
<artifactId>springfox-swagger2</artifactId>
25+
<version>2.9.2</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>io.springfox</groupId>
29+
<artifactId>springfox-swagger-ui</artifactId>
30+
<version>2.9.2</version>
31+
</dependency>
32+
<!-- Test -->
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-devtools</artifactId>
41+
<scope>runtime</scope>
42+
<optional>true</optional>
43+
</dependency>
44+
</dependencies>
45+
46+
<build>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-maven-plugin</artifactId>
51+
<configuration>
52+
<fork>true</fork>
53+
</configuration>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
58+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
package com.neo;
3+
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
8+
@SpringBootApplication
9+
public class SwaggerApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(SwaggerApplication.class, args);
13+
}
14+
15+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.neo.config;
2+
3+
import io.swagger.annotations.ApiModel;
4+
import io.swagger.annotations.ApiModelProperty;
5+
6+
/**
7+
* 通用响应对象
8+
*/
9+
@ApiModel(description = "响应对象")
10+
public class BaseResult<T> {
11+
private static final int SUCCESS_CODE = 0;
12+
private static final String SUCCESS_MESSAGE = "成功";
13+
14+
@ApiModelProperty(value = "响应码", name = "code", required = true, example = "" + SUCCESS_CODE)
15+
private int code;
16+
17+
@ApiModelProperty(value = "响应消息", name = "msg", required = true, example = SUCCESS_MESSAGE)
18+
private String msg;
19+
20+
@ApiModelProperty(value = "响应数据", name = "data")
21+
private T data;
22+
23+
private BaseResult(int code, String msg, T data) {
24+
this.code = code;
25+
this.msg = msg;
26+
this.data = data;
27+
}
28+
29+
private BaseResult() {
30+
this(SUCCESS_CODE, SUCCESS_MESSAGE);
31+
}
32+
33+
private BaseResult(int code, String msg) {
34+
this(code, msg, null);
35+
}
36+
37+
private BaseResult(T data) {
38+
this(SUCCESS_CODE, SUCCESS_MESSAGE, data);
39+
}
40+
41+
public static <T> BaseResult<T> success() {
42+
return new BaseResult<>();
43+
}
44+
45+
public static <T> BaseResult<T> successWithData(T data) {
46+
return new BaseResult<>(data);
47+
}
48+
49+
public static <T> BaseResult<T> failWithCodeAndMsg(int code, String msg) {
50+
return new BaseResult<>(code, msg, null);
51+
}
52+
53+
public static <T> BaseResult<T> buildWithParam(ResponseParam param) {
54+
return new BaseResult<>(param.getCode(), param.getMsg(), null);
55+
}
56+
57+
public int getCode() {
58+
return code;
59+
}
60+
61+
public void setCode(int code) {
62+
this.code = code;
63+
}
64+
65+
public String getMsg() {
66+
return msg;
67+
}
68+
69+
public void setMsg(String msg) {
70+
this.msg = msg;
71+
}
72+
73+
public T getData() {
74+
return data;
75+
}
76+
77+
public void setData(T data) {
78+
this.data = data;
79+
}
80+
81+
82+
83+
public static class ResponseParam {
84+
private int code;
85+
private String msg;
86+
87+
private ResponseParam(int code, String msg) {
88+
this.code = code;
89+
this.msg = msg;
90+
}
91+
92+
public static ResponseParam buildParam(int code, String msg) {
93+
return new ResponseParam(code, msg);
94+
}
95+
96+
public int getCode() {
97+
return code;
98+
}
99+
100+
public void setCode(int code) {
101+
this.code = code;
102+
}
103+
104+
public String getMsg() {
105+
return msg;
106+
}
107+
108+
public void setMsg(String msg) {
109+
this.msg = msg;
110+
}
111+
}
112+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.neo.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import springfox.documentation.builders.ApiInfoBuilder;
6+
import springfox.documentation.builders.PathSelectors;
7+
import springfox.documentation.builders.RequestHandlerSelectors;
8+
import springfox.documentation.service.ApiInfo;
9+
import springfox.documentation.service.Contact;
10+
import springfox.documentation.spi.DocumentationType;
11+
import springfox.documentation.spring.web.plugins.Docket;
12+
import springfox.documentation.swagger2.annotations.EnableSwagger2;
13+
14+
@Configuration
15+
@EnableSwagger2
16+
public class SwaggerConfig {
17+
18+
@Bean
19+
public Docket api() {
20+
return new Docket(DocumentationType.SWAGGER_2)
21+
.apiInfo(apiInfo())
22+
.select()
23+
// 自行修改为自己的包路径
24+
.apis(RequestHandlerSelectors.basePackage("com.neo.controller"))
25+
.paths(PathSelectors.any())
26+
.build();
27+
}
28+
29+
private ApiInfo apiInfo() {
30+
return new ApiInfoBuilder()
31+
.title("客户管理")
32+
.description("客户管理中心 API 1.0 操作文档")
33+
//服务条款网址
34+
.termsOfServiceUrl("http://www.ityouknow.com/")
35+
.version("1.0")
36+
.contact(new Contact("纯洁的微笑", "http://www.ityouknow.com/", "ityouknow@126.com"))
37+
.build();
38+
}
39+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
package com.neo.controller;
3+
4+
import com.neo.config.BaseResult;
5+
import com.neo.model.Message;
6+
import com.neo.repository.MessageRepository;
7+
import io.swagger.annotations.*;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.web.bind.annotation.*;
10+
11+
import java.util.List;
12+
13+
@Api(value = "消息", description = "消息操作 API", position = 100, protocols = "http")
14+
@RestController
15+
@RequestMapping("/")
16+
public class MessageController {
17+
18+
@Autowired
19+
private MessageRepository messageRepository;
20+
21+
@ApiOperation(
22+
value = "消息列表",
23+
notes = "完整的消息内容列表",
24+
produces="application/json, application/xml",
25+
consumes="application/json, application/xml",
26+
response = List.class)
27+
@GetMapping(value = "messages")
28+
public List<Message> list() {
29+
List<Message> messages = this.messageRepository.findAll();
30+
return messages;
31+
}
32+
33+
@ApiOperation(
34+
value = "添加消息",
35+
notes = "根据参数创建消息"
36+
)
37+
@ApiImplicitParams({
38+
@ApiImplicitParam(name = "id", value = "消息 ID", required = true, dataType = "Long", paramType = "query"),
39+
@ApiImplicitParam(name = "text", value = "正文", required = true, dataType = "String", paramType = "query"),
40+
@ApiImplicitParam(name = "summary", value = "摘要", required = false, dataType = "String", paramType = "query"),
41+
})
42+
@PostMapping(value = "message")
43+
public Message create(Message message) {
44+
System.out.println("message===="+message.toString());
45+
message = this.messageRepository.save(message);
46+
return message;
47+
}
48+
49+
@ApiOperation(
50+
value = "修改消息",
51+
notes = "根据参数修改消息"
52+
)
53+
@PutMapping(value = "message")
54+
@ApiResponses({
55+
@ApiResponse(code = 100, message = "请求参数有误"),
56+
@ApiResponse(code = 101, message = "未授权"),
57+
@ApiResponse(code = 103, message = "禁止访问"),
58+
@ApiResponse(code = 104, message = "请求路径不存在"),
59+
@ApiResponse(code = 200, message = "服务器内部错误")
60+
})
61+
public Message modify(Message message) {
62+
Message messageResult=this.messageRepository.update(message);
63+
return messageResult;
64+
}
65+
66+
@PatchMapping(value="/message/text")
67+
public BaseResult<Message> patch(Message message) {
68+
Message messageResult=this.messageRepository.updateText(message);
69+
return BaseResult.successWithData(messageResult);
70+
}
71+
72+
@GetMapping(value = "message/{id}")
73+
public Message get(@PathVariable Long id) {
74+
Message message = this.messageRepository.findMessage(id);
75+
return message;
76+
}
77+
78+
@DeleteMapping(value = "message/{id}")
79+
public void delete(@PathVariable("id") Long id) {
80+
this.messageRepository.deleteMessage(id);
81+
}
82+
83+
84+
85+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.neo.controller;
2+
3+
import com.neo.config.BaseResult;
4+
import com.neo.model.User;
5+
import io.swagger.annotations.*;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RequestMethod;
9+
import org.springframework.web.bind.annotation.RestController;
10+
import springfox.documentation.annotations.ApiIgnore;
11+
12+
import java.util.*;
13+
14+
15+
@Api(value = "用户管理", description = "用户管理API", position = 100, protocols = "http")
16+
@RestController
17+
@RequestMapping(value = "/user")
18+
public class UserController {
19+
static Map<Long, User> users = Collections.synchronizedMap(new HashMap<>());
20+
21+
@ApiOperation(value = "获取用户列表", notes = "查询用户列表")
22+
@RequestMapping(value = {""}, method = RequestMethod.GET)
23+
@ApiResponses({
24+
@ApiResponse(code = 100, message = "异常数据")
25+
})
26+
public List<User> getUserList() {
27+
return new ArrayList<>(users.values());
28+
}
29+
30+
@ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
31+
@ApiImplicitParams({
32+
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "query"),
33+
@ApiImplicitParam(name = "name", value = "用户名", required = true, dataType = "String", paramType = "query"),
34+
@ApiImplicitParam(name = "age", value = "年龄", required = true, dataType = "String", paramType = "query"),
35+
@ApiImplicitParam(name = "ipAddr", value = "ip哟", required = false, dataType = "String", paramType = "query")
36+
})
37+
@RequestMapping(value = "", method = RequestMethod.POST)
38+
public BaseResult<User> postUser(@ApiIgnore User user) {
39+
users.put(user.getId(), user);
40+
return BaseResult.successWithData(user);
41+
}
42+
43+
@ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
44+
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
45+
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
46+
public User getUser(@PathVariable Long id) {
47+
return users.get(id);
48+
}
49+
50+
@ApiOperation(value = "更新用户信息", notes = "根据用户ID更新信息")
51+
@ApiImplicitParams({
52+
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "query"),
53+
@ApiImplicitParam(name = "name", value = "用户名", required = true, dataType = "String", paramType = "query"),
54+
@ApiImplicitParam(name = "age", value = "年龄", required = true, dataType = "String", paramType = "query")
55+
})
56+
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
57+
public BaseResult<User> putUser(@PathVariable Long id, @ApiIgnore User user) {
58+
User u = users.get(id);
59+
u.setName(user.getName());
60+
u.setAge(user.getAge());
61+
users.put(id, u);
62+
return BaseResult.successWithData(u);
63+
}
64+
65+
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
66+
public String deleteUser(@PathVariable Long id) {
67+
users.remove(id);
68+
return "success";
69+
}
70+
71+
@RequestMapping(value = "/ignoreMe/{id}", method = RequestMethod.DELETE)
72+
public String ignoreMe(@PathVariable Long id) {
73+
users.remove(id);
74+
return "success";
75+
}
76+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /