1. 程式人生 > >【原創】Sagger使用

【原創】Sagger使用

system 地址 配置 true api logs response local resp

Swagger使用

1. Spring MVC配置文件中的配置

<mvc:annotation-driven/>

<context:component-scan base-package="com.demo"/>

設置使用註解的類所在的jar,只加載controller

<mvc:default-servlet-handler />

使用 Swagger Restful API文檔時,添加此註解

2. maven依賴

<properties>

<spring.version>4.2.6.RELEASE</spring.version>

<servlet.version>3.1.0</servlet.version>

<swagger2.version>2.5.0</swagger2.version>

</properties>

<dependencies>

<!-- swagger開始 -->

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox

-swagger2</artifactId>

<version>${swagger2.version}</version>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

<version>${swagger2.version}</version>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-staticdocs</artifactId>

<version>${swagger2.version}</version>

</dependency>

<!-- swagger結束 -->

<!--spring-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

<version>${spring.version}</version>

</dependency>

<!--web-->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<version>${servlet.version}</version>

<scope>provided</scope>

</dependency>

</dependencies>

3. Swagger註解配置

@RequestMapping(value = "/add", method = RequestMethod.POST)

@ApiOperation(value = "添加用戶", notes = "增加用戶")

public Result<UserVo> add(@ApiParam(name = "userName",value = "用戶昵稱",required = true)@RequestParam(name = "userName",required = true)String userName,

@ApiParam(name = "mobile",value = "手機",required = true)@RequestParam(name = "mobile",required = true)String mobile,

@ApiParam(required = true, name = "email", value = "郵箱") @RequestParam(name = "email", required = true) String email ) {

UserVo userVo=new UserVo();

userVo.setUserId(System.currentTimeMillis());

userVo.setUserName(userName);

return new Result<UserVo>(Constants.SUCCESS,Constants.MSG_SUCCESS,userVo);

}

@ApiOperation(value = "創建用戶", notes = "根據UserVo對象創建用戶")

@RequestMapping(value = "/create", method = RequestMethod.POST)

public String postUser(@ApiParam(required = true, name = "userVo", value = "實體UserVo") @RequestBody UserVo userVo) {

return "success";

}

@RequestMapping(value = "/getUser", method = RequestMethod.GET)

@ApiOperation(value = "獲取用戶", notes = "根據UserVo獲取用戶")

@ResponseBody

public Result<UserVo> getUser() {

UserVo userVo=new UserVo();

userVo.setUserId(System.currentTimeMillis());

userVo.setUserName("UserName");

return new Result<UserVo>(Constants.SUCCESS,Constants.MSG_SUCCESS,userVo);

}

SwaggerConfig類編寫示例

@Configuration

@EnableSwagger2

public class SwaggerConfig {

@Bean

public Docket userApi() {

return new Docket(DocumentationType.SWAGGER_2)

.groupName("用戶")

.select() // 選擇那些路徑和api會生成document

.apis(RequestHandlerSelectors.basePackage("com.zyx.controller"))

.paths(PathSelectors.any()) // 對所有路徑進行監控

.build()

.apiInfo(userInfo());

}

private ApiInfo userInfo() {

ApiInfo apiInfo = new ApiInfo("用戶相關接口",//大標題

"用戶有關的接口,包括增加刪除用戶",//小標題

"0.1",//版本

"杭州",

new Contact("zyx", "", ""),// 作者

"swagger url",//鏈接顯示文字

""//網站鏈接

);

return apiInfo;

}

@Bean

public Docket otherApi() {

return new Docket(DocumentationType.SWAGGER_2)

.groupName("其它")

.select() // 選擇那些路徑和api會生成document

.apis(RequestHandlerSelectors.basePackage("com.zyx.controller"))

.paths(PathSelectors.any()) // 對所有路徑進行監控

.build()

.apiInfo(otherInfo());

}

private ApiInfo otherInfo() {

ApiInfo apiInfo = new ApiInfo("其它相關接口",//大標題

"其它有關的接口,包括增加刪除其它",//小標題

"0.1",//版本

"杭州",

new Contact("zyx_other", "", ""),// 作者

"點擊",//鏈接顯示文字

""//網站鏈接

);

return apiInfo;

}

}

:SwaggerConfigSpring註入管理 <bean class="com.demo.config.SwaggerConfig"/>

@ApiModel(value = "用戶信息")

public class UserVo {

@ApiModelProperty(value = "用戶id", required = true)

private long userId;

@ApiModelProperty(value = "昵稱", required = true)

private String userName;

public long getUserId() {

return userId;

}

public void setUserId(long userId) {

this.userId = userId;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

}

4. 界面展示

技術分享

技術分享

技術分享

在瀏覽器輸入地址:http://localhost:8080/swagger-ui.html

【原創】Sagger使用