正在显示
5 个修改的文件
包含
102 行增加
和
0 行删除
@@ -19,6 +19,7 @@ | @@ -19,6 +19,7 @@ | ||
19 | <spring-cloud.version>Greenwich.BUILD-SNAPSHOT</spring-cloud.version> | 19 | <spring-cloud.version>Greenwich.BUILD-SNAPSHOT</spring-cloud.version> |
20 | <fastjson_version>1.2.28</fastjson_version> | 20 | <fastjson_version>1.2.28</fastjson_version> |
21 | <lombok_sersion>1.18.6</lombok_sersion> | 21 | <lombok_sersion>1.18.6</lombok_sersion> |
22 | + <swagger2_version>2.9.2</swagger2_version> | ||
22 | </properties> | 23 | </properties> |
23 | 24 | ||
24 | <dependencies> | 25 | <dependencies> |
@@ -85,8 +86,54 @@ | @@ -85,8 +86,54 @@ | ||
85 | </exclusion> | 86 | </exclusion> |
86 | </exclusions> | 87 | </exclusions> |
87 | </dependency> | 88 | </dependency> |
89 | + | ||
88 | <!--spring boot--> | 90 | <!--spring boot--> |
89 | <!-- tools--> | 91 | <!-- tools--> |
92 | + <!--集成swagger2,下面两个spring的配置解决springboot包 与swagger2的包冲突问题,生产部署的时候记得要去掉此swagger2的配置,开发环境开启--> | ||
93 | + <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> | ||
94 | + <dependency> | ||
95 | + <groupId>org.springframework.plugin</groupId> | ||
96 | + <artifactId>spring-plugin-core</artifactId> | ||
97 | + <version>1.2.0.RELEASE</version><!--$NO-MVN-MAN-VER$--> | ||
98 | + </dependency> | ||
99 | + <dependency> | ||
100 | + <groupId>org.springframework.plugin</groupId> | ||
101 | + <artifactId>spring-plugin-metadata</artifactId> | ||
102 | + <version>1.2.0.RELEASE</version><!--$NO-MVN-MAN-VER$--> | ||
103 | + </dependency> | ||
104 | + <dependency> | ||
105 | + <groupId>io.springfox</groupId> | ||
106 | + <artifactId>springfox-swagger-ui</artifactId> | ||
107 | + <version>${swagger2_version}</version> | ||
108 | + <exclusions> | ||
109 | + <exclusion> | ||
110 | + <groupId>org.springframework.plugin</groupId> | ||
111 | + <artifactId>spring-plugin-core</artifactId> | ||
112 | + </exclusion> | ||
113 | + <exclusion> | ||
114 | + <groupId>org.springframework.plugin</groupId> | ||
115 | + <artifactId>spring-plugin-metadata</artifactId> | ||
116 | + </exclusion> | ||
117 | + </exclusions> | ||
118 | + </dependency> | ||
119 | + <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> | ||
120 | + <dependency> | ||
121 | + <groupId>io.springfox</groupId> | ||
122 | + <artifactId>springfox-swagger2</artifactId> | ||
123 | + <version>${swagger2_version}</version> | ||
124 | + <!--2.9.2集成的是M1的这两个包,跟现在用的Greenwich.BUILD-SNAPSHOT的版本的SPRING boot2的版本包有冲突。去掉--> | ||
125 | + <exclusions> | ||
126 | + <exclusion> | ||
127 | + <groupId>org.springframework.plugin</groupId> | ||
128 | + <artifactId>spring-plugin-core</artifactId> | ||
129 | + </exclusion> | ||
130 | + <exclusion> | ||
131 | + <groupId>org.springframework.plugin</groupId> | ||
132 | + <artifactId>spring-plugin-metadata</artifactId> | ||
133 | + </exclusion> | ||
134 | + </exclusions> | ||
135 | + </dependency> | ||
136 | + <!--集成swagger2--> | ||
90 | <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> | 137 | <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> |
91 | <dependency> | 138 | <dependency> |
92 | <groupId>org.projectlombok</groupId> | 139 | <groupId>org.projectlombok</groupId> |
@@ -23,3 +23,4 @@ | @@ -23,3 +23,4 @@ | ||
23 | * 集成lombok,简化部分代码录入,比如实体类,方便实体及表结构修改,敏捷开发必用,使用方法见[lombok集成使用说明](https://jingyan.baidu.com/article/0a52e3f4e53ca1bf63ed725c.html) | 23 | * 集成lombok,简化部分代码录入,比如实体类,方便实体及表结构修改,敏捷开发必用,使用方法见[lombok集成使用说明](https://jingyan.baidu.com/article/0a52e3f4e53ca1bf63ed725c.html) |
24 | ) | 24 | ) |
25 | * 集成了单文件、多文件上传接口 | 25 | * 集成了单文件、多文件上传接口 |
26 | +* 集成了swagger2,开发环境开启此POM配置,生产环境记得去掉。包冲突问题看POM配置文件。 |
1 | +package com.tianbo.warehouse; | ||
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 Swagger2 { | ||
17 | + //swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等 | ||
18 | + @Bean | ||
19 | + public Docket createRestApi() { | ||
20 | + return new Docket(DocumentationType.SWAGGER_2) | ||
21 | + .apiInfo(apiInfo()) | ||
22 | + .select() | ||
23 | + //为当前包路径 | ||
24 | + .apis(RequestHandlerSelectors.basePackage("com.tianbo.warehouse.controller")) | ||
25 | + .paths(PathSelectors.any()) | ||
26 | + .build(); | ||
27 | + } | ||
28 | + //构建 api文档的详细信息函数,注意这里的注解引用的是哪个 | ||
29 | + private ApiInfo apiInfo() { | ||
30 | + return new ApiInfoBuilder() | ||
31 | + //页面标题 | ||
32 | + .title("Spring Boot 测试使用 Swagger2 构建RESTful API") | ||
33 | + //创建人 | ||
34 | + .contact(new Contact("MarryFeng", "http://www.baidu.com", "")) | ||
35 | + //版本号 | ||
36 | + .version("1.0") | ||
37 | + //描述 | ||
38 | + .description("API 描述") | ||
39 | + .build(); | ||
40 | + } | ||
41 | + | ||
42 | + | ||
43 | +} |
@@ -7,6 +7,10 @@ import com.tianbo.warehouse.controller.response.ResultJson; | @@ -7,6 +7,10 @@ import com.tianbo.warehouse.controller.response.ResultJson; | ||
7 | import com.tianbo.warehouse.model.USERS; | 7 | import com.tianbo.warehouse.model.USERS; |
8 | import com.tianbo.warehouse.model.UserRole; | 8 | import com.tianbo.warehouse.model.UserRole; |
9 | import com.tianbo.warehouse.service.UserService; | 9 | import com.tianbo.warehouse.service.UserService; |
10 | +import io.swagger.annotations.Api; | ||
11 | +import io.swagger.annotations.ApiImplicitParam; | ||
12 | +import io.swagger.annotations.ApiImplicitParams; | ||
13 | +import io.swagger.annotations.ApiOperation; | ||
10 | import org.springframework.beans.factory.annotation.Autowired; | 14 | import org.springframework.beans.factory.annotation.Autowired; |
11 | import org.springframework.security.core.context.SecurityContextHolder; | 15 | import org.springframework.security.core.context.SecurityContextHolder; |
12 | import org.springframework.security.core.userdetails.UserDetails; | 16 | import org.springframework.security.core.userdetails.UserDetails; |
@@ -18,11 +22,16 @@ import javax.servlet.http.HttpServletResponse; | @@ -18,11 +22,16 @@ import javax.servlet.http.HttpServletResponse; | ||
18 | import javax.validation.Valid; | 22 | import javax.validation.Valid; |
19 | 23 | ||
20 | @RestController | 24 | @RestController |
25 | +@RequestMapping("/user") | ||
26 | +@Api("swaggerDemoController相关的api") | ||
21 | public class UserController { | 27 | public class UserController { |
22 | 28 | ||
23 | @Autowired | 29 | @Autowired |
24 | UserService userService; | 30 | UserService userService; |
25 | 31 | ||
32 | + @ApiOperation(value = "查询用户列表及信息", notes = "查询用户列表及单个用户信息") | ||
33 | + @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "分页-当前页", required = false, dataType = "int",defaultValue = "1"), | ||
34 | + @ApiImplicitParam(name = "pageSize", value = "分页-每页显示多少条", required = false, dataType = "int",defaultValue = "5")}) | ||
26 | @GetMapping("/user/list") | 35 | @GetMapping("/user/list") |
27 | public PageInfo<USERS> list(@RequestParam(value = "pageNum",required = false,defaultValue = "1") | 36 | public PageInfo<USERS> list(@RequestParam(value = "pageNum",required = false,defaultValue = "1") |
28 | int pageNum, | 37 | int pageNum, |
@@ -4,8 +4,10 @@ import org.junit.Test; | @@ -4,8 +4,10 @@ import org.junit.Test; | ||
4 | import org.junit.runner.RunWith; | 4 | import org.junit.runner.RunWith; |
5 | import org.springframework.boot.test.context.SpringBootTest; | 5 | import org.springframework.boot.test.context.SpringBootTest; |
6 | import org.springframework.test.context.junit4.SpringRunner; | 6 | import org.springframework.test.context.junit4.SpringRunner; |
7 | +import org.springframework.test.context.web.WebAppConfiguration; | ||
7 | 8 | ||
8 | @RunWith(SpringRunner.class) | 9 | @RunWith(SpringRunner.class) |
10 | +@WebAppConfiguration | ||
9 | //解决mvn clean package打包项目websocketjavax.websocket.server.ServerContainer not available报错的问题 | 11 | //解决mvn clean package打包项目websocketjavax.websocket.server.ServerContainer not available报错的问题 |
10 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | 12 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
11 | public class WarehouseApplicationTests { | 13 | public class WarehouseApplicationTests { |
-
请 注册 或 登录 后发表评论