關於springmvc4整合swagger2配置和jar依賴

jar依賴

可從從 https://mvnrepository.com/ 網站進行下載或者mavne 導入

<dependency>
	<groupId>org.codehaus.jackson</groupId>
	<artifactId>jackson-mapper-asl</artifactId>
	<version>1.9.13</version>
</dependency>
<dependency>
	<groupId>org.codehaus.jackson</groupId>
	<artifactId>jackson-core-asl</artifactId>
	<version>1.9.13</version>
</dependency>
<dependency>
	 <groupId>com.fasterxml.jackson.core</groupId>
	 <artifactId>jackson-core</artifactId>
	 <version>2.9.2</version>
</dependency>
<dependency>
	 <groupId>com.fasterxml.jackson.core</groupId>
	 <artifactId>jackson-annotations</artifactId>
	 <version>2.9.2</version>
</dependency>
<dependency>
	 <groupId>com.fasterxml.jackson.core</groupId>
	 <artifactId>jackson-databind</artifactId>
	 <version>2.9.2</version>
</dependency>
<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>
<!-- google -->
<dependency>
	<groupId>com.google.guava</groupId>
	<artifactId>guava</artifactId>
	<version>22.0</version>
</dependency>

web.xml 配置

<servlet>
	<servlet-name>FrontMvc</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/config/spring-front-mvc.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>


<servlet-mapping>
	<servlet-name>FrontMvc</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>

spring-mvc.xml 配置

<!-- 掃描Controller -->
<context:component-scan base-package="com.swagger" />
	
<!-- 靜態資源釋放 -->
<mvc:resources mapping="/swagger/**" location="/swagger/"/>

SwaggerConfig配置類

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import springfox.documentation.builders.ApiInfoBuilder;
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 聲明啓動Swagger2
 * @EnableWebMvc 聲明啓動mvc 配置此註解通過包掃描,不需要進行spring bean xml 配置
 * */
@Configuration 
@EnableSwagger2 
@EnableWebMvc 
public class SwaggerConfig {
	
	@Bean
	public Docket api() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
				.apis(RequestHandlerSelectors.basePackage("com.coupon.action"))// 掃描的包路徑
				.build();
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("你的項目 API接口").description("此API提供接口調用").license("License Version 2.0")
				.version("2.0").build();
	}
}

 

controller配置


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.bean.Person;
import com.service.PersonService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;

@RestController
@RequestMapping("/person")
@Api(tags = "人員接口",description="人員文檔說明",hidden=true)
public class PersonController {

	@Autowired
	private PersonService personService;
	
	@RequestMapping(value="/findAll")
	@ApiOperation(value="查詢所有的人員",notes="查詢所有的人員接口說明")
	@ApiImplicitParams({
		@ApiImplicitParam(name="month",value="年月,格式爲:201801",dataType="String", paramType = "query"),
		@ApiImplicitParam(name="pageNo",value="頁碼",dataType="Integer", paramType = "query"),
		@ApiImplicitParam(name="pageSize",value="每頁條數",dataType="Integer", paramType = "query"),
		@ApiImplicitParam(name="name",value="業務員名稱",dataType="String", paramType = "query"),
		@ApiImplicitParam(name="orderType",value="排序類型",dataType="String", paramType = "query"),
	})
	@ApiResponse(response=Person.class, code = 200, message = "接口返回對象參數")
	@ResponseBody
	public List<Person> findAll(HttpServletRequest request,String month,Integer pageNo, Integer pageSize,String name,String orderType) {
		List<Person> list = personService.selectAll();
		return list;
	}
	
}

 

 

Swagger-UI配置

Swagger掃描解析得到的是一個json文檔,對於用戶不太友好。

下面介紹swagger-ui,它能夠友好的展示解析得到的接口說明內容。


從https://github.com/swagger-api/swagger-ui 獲取其所有的 dist 目錄下東西放到需要集成的項目裏,本文放入 src/main/webapp/swagger/ 目錄下。

修改swagger目錄下 index.html或者swagger-ui.html

默認是從連接http://petstore.swagger.io/v2/swagger.json 獲取 API 的 JSON,這裏需要將url值修改爲http://{ip}:{port}/{projectName}/api-docs的形式,{}中的值根據自身情 況填寫。

比如我的url值爲:http://localhost:8080/v2/api-docs

if (url && url.length > 1) {
    url = decodeURIComponent(url[1]);
  } else {
    url = "http://localhost:8080/v2/api-docs";
  }

因爲swagger-ui項目都是靜態資源,restful形式的攔截方法會將靜態資源進行攔截處理,所以在springmvc配置文件中需要配置對靜態文件的處理方式。

幾款漂亮的Swagger UI 框架

https://gitee.com/caspar-chen/Swagger-UI-layer

https://gitee.com/xiaoym/swagger-bootstrap-ui

 

註解全參數

 

以下是swagger2註解中的全參數,有興趣可以都試試

@Api

Api 標記可以標記一個Controller類做爲swagger 文檔資源,使用方式

屬性名稱

備註

value

url的路徑值

tags

如果設置這個值、value的值會被覆蓋

description

對api資源的描述

basePath

基本路徑可以不配置

position

如果配置多個Api 想改變顯示的順序位置

produces

For example, “application/json, application/xml”

consumes

For example, “application/json, application/xml”

protocols

Possible values: http, https, ws, wss.

authorizations

高級特性認證時配置

hidden

配置爲true 將在文檔中隱藏

@ApiOperation每一個url資源的定義,使用方式

屬性名稱

備註

value

url的路徑值

tags

如果設置這個值、value的值會被覆蓋

description

對api資源的描述

basePath

基本路徑可以不配置

position

如果配置多個Api 想改變顯示的順序位置

produces

For example, “application/json, application/xml”

consumes

For example, “application/json, application/xml”

protocols

Possible values: http, https, ws, wss.

authorizations

高級特性認證時配置

hidden

配置爲true 將在文檔中隱藏

response

返回的對象

responseContainer

這些對象是有效的 “List”, “Set” or “Map”.,其他無效

httpMethod

“GET”, “HEAD”, “POST”, “PUT”, “DELETE”, “OPTIONS” and “PATCH”

code

http的狀態碼 默認 200

extensions

擴展屬性

@ApiParam標記

 

public ResponseEntity createUser(@RequestBody @ApiParam(value = “user”, required = true) User user)

屬性名稱

備註

name

屬性名稱

value

屬性值

defaultValue

默認屬性值

allowableValues

可以不配置

required

是否屬性必填

access

不過多描述

allowMultiple

默認爲false

hidden

隱藏該屬性

example

舉例子

@ApiImplicitParam對容器的描述

屬性名稱

備註

name

屬性名稱

value

屬性值

defaultValue

默認值

allowableValues

可以不可配置

required

是否屬性必填

access

不可過多描述

allowMutiple

默認爲false

dataType

數據類型

paramType

參數類型

@ApiResponse

屬性名稱

備註

code

http的狀態碼

message

描述

response

默認響應類 Void

reference

參考ApiOperation中配置

responseHeaders

參考 ResponseHeader 屬性配置說明

responseContainer

參考ApiOperation中配置

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章