swagger2介面釋出demo
1.目的:使用Swagger2釋出介面,ui可操作
2.專案結構
3. 程式碼
3.1 介面類qinfeng.zheng.api.controller.DemoController
package qinfeng.zheng.api.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import qinfeng.zheng.api.entity.UserEntity;
@Api(value = "會員介面")
@RestController
public class DemoController {
@ApiOperation(value = "swagger介面測試demo", nickname = "swagger介面測試demo暱稱")
@GetMapping("/getDemo")
public String getDemo() {
return "getDemo方法呼叫成功...";
}
@ApiOperation(value = "獲取會員資訊介面", nickname = "根據userName獲取使用者相關資訊")
@ApiImplicitParam(name = "userName", value = "使用者名稱稱", required = true, dataType = "String")
@PostMapping("/postMember")
public String postMember(@RequestParam String userName) {
return userName;
}
@ApiOperation(value = "新增使用者資訊", nickname = "nickname是什麼", notes = "notes是什麼", produces = "application/json")
@PostMapping("/postUser")
@ResponseBody
@ApiImplicitParam(paramType = "query", name = "userId", value = "使用者id", required = true, dataType = "int")
public UserEntity postUser(@RequestBody UserEntity user, @RequestParam("userId") int userId) { // 這裡用包裝類竟然報錯
if (user.getId() == userId) {
return user;
}
return new UserEntity();
}
@ApiOperation(value = "新增使用者資訊", nickname = "哈哈測試", notes = "哈哈測試新增使用者", produces = "application/json")
@PostMapping("/addUser")
@ResponseBody
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "userName", value = "使用者姓名", required = true, dataType = "String"),
@ApiImplicitParam(paramType = "query", name = "id", value = "使用者id", required = true, dataType = "int") })
public UserEntity addUser(String userName, int id) {
UserEntity userEntity = new UserEntity();
userEntity.setName(userName);
userEntity.setId(id);
return userEntity;
}
}
3.2 實體類qinfeng.zheng.api.entity.UserEntity
package qinfeng.zheng.api.entity; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; /** * 建立時間: 23:09 2018/9/19 * 修改時間: * 編碼人員: ZhengQf * 版本: 0.0.1 * 功能描述: */ @ApiModel(value = "使用者模型") public class UserEntity { @ApiModelProperty(value="id" ,required= true,example = "123") private Integer id; @ApiModelProperty(value="使用者姓名" ,required=true,example = "鄭欽鋒") private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "DemoDoctor [id=" + id + ", name=" + name + "]"; } }
3.3 配置類qinfeng.zheng.config.SwaggerConfig
package qinfeng.zheng.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; /** * swagger2的配置類 */ @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select() // api掃包範圍 .apis(RequestHandlerSelectors.basePackage("qinfeng.zheng.api")).paths(PathSelectors.any()).build(); } /** * 建立該API的基本資訊(這些基本資訊會展現在文件頁面中) * 訪問地址:http://專案實際地址/swagger-ui.html * @return */ private ApiInfo apiInfo() { return new ApiInfoBuilder().title("Swagger介面釋出測試").description("測試|Swagger介面功能") .termsOfServiceUrl("http://www.baidu.com") .version("1.0").build(); } }
3.4 啟動類qinfeng.zheng.AppSwagger
package qinfeng.zheng; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class AppSwagger { public static void main(String[] args) { SpringApplication.run(AppSwagger.class, args); } }
3.5 application.yml
server: port: 8080 spring: application: name: swagger
3.6 maven依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>qinfeng.zheng</groupId> <artifactId>springboot-swagger-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot-swagger-demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <!-- SpringBoot整合Web元件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> </dependencies> </project>
4. 啟動專案
4.1 專案啟動成功之後,瀏覽器訪問http://localhost:8080/swagger-ui.html
4.2 測試addUser介面
點選Execute提交請求,
請求成功,其它介面可自行測試,皆正常!!!