Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0687626

Browse files
committed
swagger2配置
1 parent fd51ad7 commit 0687626

File tree

5 files changed

+96
-12
lines changed

5 files changed

+96
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ redis缓存,
1919
xml注入spring boot无法扫描到的bean,
2020
actuator监控,
2121
kafka消息队列,
22-
22+
Swagger2配置

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@
124124
<version>1.2.1</version>
125125
</dependency>
126126

127+
<!--swagger start-->
128+
<dependency>
129+
<groupId>io.springfox</groupId>
130+
<artifactId>springfox-swagger-ui</artifactId>
131+
<version>2.6.1</version>
132+
</dependency>
133+
<dependency>
134+
<groupId>io.springfox</groupId>
135+
<artifactId>springfox-swagger2</artifactId>
136+
<version>2.6.1</version>
137+
</dependency>
138+
127139
<dependency>
128140
<groupId>org.jon.lv</groupId>
129141
<artifactId>Mybatis-DynamicDataSource</artifactId>

src/main/java/org/jon/lv/controller/KafkaController.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
import org.jon.lv.kafka.MsgProducer;
55
import org.jon.lv.result.ResultDO;
66
import org.springframework.beans.factory.annotation.Autowired;
7-
import org.springframework.web.bind.annotation.PathVariable;
8-
import org.springframework.web.bind.annotation.RequestMapping;
9-
import org.springframework.web.bind.annotation.RequestParam;
10-
import org.springframework.web.bind.annotation.RestController;
7+
import org.springframework.web.bind.annotation.*;
118

129
/**
1310
* @Package org.jon.lv.controller.KafkaController
@@ -24,7 +21,7 @@ public class KafkaController {
2421
@Autowired
2522
private MsgProducer msgProducer;
2623

27-
@RequestMapping("/send")
24+
@RequestMapping(value = "/send", method = RequestMethod.GET)
2825
public ResultDO<String> get(@RequestParam("content") String content){
2926
ResultDO<String> resultDO = new ResultDO<>();
3027

src/main/java/org/jon/lv/controller/UserController.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package org.jon.lv.controller;
22

3+
import io.swagger.annotations.ApiImplicitParam;
4+
import io.swagger.annotations.ApiImplicitParams;
5+
import io.swagger.annotations.ApiOperation;
6+
import io.swagger.annotations.ApiResponse;
37
import org.jon.lv.domain.User;
48
import org.jon.lv.result.ResultDO;
59
import org.jon.lv.service.UserService;
610
import org.springframework.beans.factory.annotation.Autowired;
711
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
8-
import org.springframework.web.bind.annotation.PathVariable;
9-
import org.springframework.web.bind.annotation.RequestBody;
10-
import org.springframework.web.bind.annotation.RequestMapping;
11-
import org.springframework.web.bind.annotation.RestController;
12+
import org.springframework.web.bind.annotation.*;
1213

1314
/**
1415
* @Package org.jon.lv.controller.UserController
@@ -24,15 +25,23 @@ public class UserController {
2425
@Autowired
2526
private UserService userService;
2627

27-
@RequestMapping("/get/{id}")
28+
@ApiOperation(value = "查询单个用户",notes = "根据传入id查找用户",httpMethod = "GET")
29+
@ApiResponse(code = 200,message = "Success")
30+
@RequestMapping(value ="/get/{id}", method = RequestMethod.GET)
2831
public ResultDO<User> get(@PathVariable("id")Integer id){
2932
ResultDO<User> resultDO = new ResultDO<>();
3033
resultDO.setSuccess(true);
3134
resultDO.setData(userService.getUserById(id));
3235
return resultDO;
3336
}
3437

35-
@RequestMapping("/add")
38+
// @ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")
39+
// @ApiImplicitParams({
40+
// @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
41+
// @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
42+
// })
43+
44+
@RequestMapping(value = "/add", method = RequestMethod.POST)
3645
public ResultDO<Integer> add(User user){
3746
ResultDO<Integer> resultDO = new ResultDO<>();
3847
resultDO.setSuccess(true);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.jon.lv.swagger2;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.context.request.async.DeferredResult;
7+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
8+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
9+
import springfox.documentation.builders.ApiInfoBuilder;
10+
import springfox.documentation.builders.PathSelectors;
11+
import springfox.documentation.builders.RequestHandlerSelectors;
12+
import springfox.documentation.service.ApiInfo;
13+
import springfox.documentation.service.Contact;
14+
import springfox.documentation.spi.DocumentationType;
15+
import springfox.documentation.spring.web.plugins.Docket;
16+
import springfox.documentation.swagger2.annotations.EnableSwagger2;
17+
18+
/**
19+
* @Package org.jon.lv.swagger2.SwaggerConfig
20+
* @Copyright: Copyright (c) 2016
21+
* Author lv bin
22+
* @date 2017/5/19 10:45
23+
* version V1.0.0
24+
*/
25+
@Configuration
26+
@EnableSwagger2
27+
public class SwaggerConfig extends WebMvcConfigurerAdapter {
28+
29+
//http://localhost:8098/swagger-ui.html
30+
31+
/**
32+
* 使用enableMVC注解的话,该配置必须,否则无法映射资源
33+
*/
34+
@Override
35+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
36+
registry.addResourceHandler("swagger-ui.html")
37+
.addResourceLocations("classpath:/META-INF/resources/");
38+
registry.addResourceHandler("/webjars/**")
39+
.addResourceLocations("classpath:/META-INF/resources/webjars/");
40+
41+
}
42+
43+
@Bean
44+
public Docket createRestApi() {
45+
return new Docket(DocumentationType.SWAGGER_2)
46+
.apiInfo(apiInfo())
47+
.groupName("api")
48+
.genericModelSubstitutes(DeferredResult.class)
49+
.useDefaultResponseMessages(false)
50+
.forCodeGeneration(true)
51+
.pathMapping("/")
52+
.select()
53+
.apis(RequestHandlerSelectors.basePackage("org.jon.lv.controller"))
54+
.build();
55+
}
56+
57+
private ApiInfo apiInfo() {
58+
Contact contact = new Contact("jon","http://","316476844.com");
59+
return new ApiInfoBuilder()
60+
.title("Spring Data")
61+
.description("Spring Data 学习记录")
62+
.contact(contact)
63+
.version("1.0")
64+
.build();
65+
}
66+
}

0 commit comments

Comments
 (0)