SpringBoot集成springfox-swagger2构建restful API
一、在pom.xml中引用相关依赖
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency>
二、创建swagger的配置类Swagger2Configuration.java;使用new ApiInfoBuilder()进行构造,需要什么参数就添加什么参数。当然也可以什么都添加。
package com.meritdata.swagger.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; @Configuration @EnableSwagger2 public class Swagger2Configuration { @Bean public Docket buildDocket(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(buildApiInf()) .select() .apis(RequestHandlerSelectors.basePackage("com.meritdata.swagger.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo buildApiInf(){ return new ApiInfoBuilder() .build(); /*return new ApiInfoBuilder() .title("大标题") .description("springboot swagger2") .termsOfServiceUrl("http://blog.csdn.net/u0123网址链接") .contact(new Contact("xxxx", "http://blog.csdn.net/u0123", "bokeyuan@163.com")) .build();*/ } }
三、在Controller添加相关的注解。
常用的注解如下:
- @Api()用于类名
- @ApiOperation()用于方法名
- @ApiParam()用于参数说明
- @ApiModel()用于实体类
- @ApiModelProperty用于实体类属性
更加详细的含义可以参考官方说明wiki
在controller包下创建类UserController.java;在使用对象作为参数时,可以在对象上添加相应的注解
package com.meritdata.swagger.controller; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import javax.annotation.Resource; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.meritdata.swagger.model.User; import com.meritdata.swagger.service.UserService; @Api(value="用户controller",description="用户操作",tags={"用户操作接口"}) @RestController public class UserController { @Resource private UserService userService; @ApiOperation("获取用户信息") @GetMapping("/getUserInfo") public User getUserInfo(@ApiParam(name="id",value="用户id",required=true) Long id,@ApiParam(name="username",value="用户名") String username) { User user = userService.getUserInfo(); return user; } @ApiOperation("更改用户信息") @PostMapping("/updateUserInfo") public int updateUserInfo(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true) User user){ int num = userService.updateUserInfo(user); return num; } @ApiOperation("添加用户信息") @PostMapping("/saveUser") public String saveUser(@RequestBody @ApiParam(name="user",value="json fromat",required=true) User user) { userService.saveUser(user); return "success"; } }
package com.meritdata.swagger.model; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import java.util.List; @ApiModel(description="用户对象user") public class User { @ApiModelProperty(value="用户名",name="username") private String username; @ApiModelProperty(value="状态",name="state",required=true) private Integer state; private String password; private String nickName; private Integer isDeleted; private String[] ids; private List<String> idList; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getState() { return state; } public void setState(Integer state) { this.state = state; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String[] getIds() { return ids; } public void setIds(String[] ids) { this.ids = ids; } public List<String> getIdList() { return idList; } public void setIdList(List<String> idList) { this.idList = idList; } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } public Integer getIsDeleted() { return isDeleted; } public void setIsDeleted(Integer isDeleted) { this.isDeleted = isDeleted; } }
四、在浏览器上输入url: http://{ip}:{port}/swagger-ui.html#/


浙公网安备 33010602011771号