Swigger整合
This commit is contained in:
@@ -81,8 +81,18 @@
|
|||||||
<artifactId>pagehelper</artifactId>
|
<artifactId>pagehelper</artifactId>
|
||||||
<version>4.2.1</version>
|
<version>4.2.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!--springfox-->
|
||||||
</dependencies>
|
<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>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package com.xwolf.boot.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.spi.DocumentationType;
|
||||||
|
import springfox.documentation.spring.web.plugins.Docket;
|
||||||
|
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* swigger 整合管理API接口
|
||||||
|
* 参考<url>http://www.jianshu.com/p/8033ef83a8ed</url>
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author xwolf
|
||||||
|
* @date 2017-02-26 20:12
|
||||||
|
* @since V1.0.0
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@EnableSwagger2
|
||||||
|
public class SwiggerConfig {
|
||||||
|
@Bean
|
||||||
|
public Docket createRestApi() {
|
||||||
|
return new Docket(DocumentationType.SWAGGER_2)
|
||||||
|
.apiInfo(apiInfo())
|
||||||
|
.select()
|
||||||
|
.apis(RequestHandlerSelectors.basePackage("com.xwolf.boot.controller"))
|
||||||
|
.paths(PathSelectors.any())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private ApiInfo apiInfo() {
|
||||||
|
return new ApiInfoBuilder()
|
||||||
|
.title("Spring Boot中使用Swagger2构建RESTful APIs")
|
||||||
|
.description("更多Spring Boot相关文章请关注:http://blog.didispace.com/")
|
||||||
|
.termsOfServiceUrl("http://blog.didispace.com/")
|
||||||
|
.contact("程序猿")
|
||||||
|
.version("1.0")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package com.xwolf.boot.controller;
|
||||||
|
|
||||||
|
import com.xwolf.boot.entity.User;
|
||||||
|
import io.swagger.annotations.ApiImplicitParam;
|
||||||
|
import io.swagger.annotations.ApiImplicitParams;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author xwolf
|
||||||
|
* @date 2017-02-26 20:15
|
||||||
|
* @since V1.0.0
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping(value="/api")
|
||||||
|
public class ApiController {
|
||||||
|
static Map<Integer, User> users = Collections.synchronizedMap(new HashMap<Integer, User>());
|
||||||
|
|
||||||
|
@ApiOperation(value="获取用户列表", notes="")
|
||||||
|
@RequestMapping(value={""}, method=RequestMethod.GET)
|
||||||
|
public List<User> getUserList() {
|
||||||
|
List<User> r = new ArrayList<User>(users.values());
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiOperation(value="创建用户", notes="根据User对象创建用户")
|
||||||
|
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
|
||||||
|
@RequestMapping(value="", method=RequestMethod.POST)
|
||||||
|
public String postUser(@RequestBody User user) {
|
||||||
|
users.put(user.getUid(), user);
|
||||||
|
return "success";
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
|
||||||
|
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer")
|
||||||
|
@RequestMapping(value="/{id}", method= RequestMethod.GET)
|
||||||
|
public User getUser(@PathVariable Integer id) {
|
||||||
|
return users.get(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")
|
||||||
|
@ApiImplicitParams({
|
||||||
|
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer"),
|
||||||
|
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
|
||||||
|
})
|
||||||
|
@RequestMapping(value="/{id}", method=RequestMethod.PUT)
|
||||||
|
public String putUser(@PathVariable Integer id, @RequestBody User user) {
|
||||||
|
User u = users.get(id);
|
||||||
|
u.setUname(user.getUname());
|
||||||
|
users.put(id, u);
|
||||||
|
return "success";
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiOperation(value="删除用户", notes="根据url的id来指定删除对象")
|
||||||
|
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer")
|
||||||
|
@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
|
||||||
|
public String deleteUser(@PathVariable Integer id) {
|
||||||
|
users.remove(id);
|
||||||
|
return "success";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,7 +28,7 @@ public class UserController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private IUserService userService;
|
private IUserService userService;
|
||||||
|
|
||||||
@RequestMapping("add")
|
@RequestMapping(value = "add",method = RequestMethod.POST)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public String insert(@RequestParam(value = "name",defaultValue = "")String name){
|
public String insert(@RequestParam(value = "name",defaultValue = "")String name){
|
||||||
User user = new User();
|
User user = new User();
|
||||||
@@ -38,7 +38,7 @@ public class UserController {
|
|||||||
return userService.insert(user);
|
return userService.insert(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("list/{start}/{size}")
|
@RequestMapping(value = "list/{start}/{size}",method = RequestMethod.POST)
|
||||||
public PageInfo<User> getUserList(@PathVariable("start")int start,@PathVariable("size")int size){
|
public PageInfo<User> getUserList(@PathVariable("start")int start,@PathVariable("size")int size){
|
||||||
PageHelper.startPage(start,size);
|
PageHelper.startPage(start,size);
|
||||||
List<User> list= userService.getList();
|
List<User> list= userService.getList();
|
||||||
|
|||||||
Reference in New Issue
Block a user