免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
spring-boot-swagger2 使用手冊(cè)

在項(xiàng)目中常用的注解說明

注解屬性備注
@Apivalue字符串可用在class頭上,class描述
 description字符串 
   @Api(value = "xxx", description = "xxx")
@ApiOperationvalue字符串可用在方法頭上.參數(shù)的描述容器
 notes字符串 
   @ApiOperation(value = "xxx", notes = "xxx")
@ApiImplicitParams{}@ApiImplicitParam數(shù)組可用在方法頭上.參數(shù)的描述容器
   @ApiImplicitParams({@ApiImplicitParam1,@ApiImplicitParam2,...})
@ApiImplicitParamname字符串 與參數(shù)命名對(duì)應(yīng)可用在@ApiImplicitParams
 value字符串參數(shù)中文描述
 required布爾值true/false
 dataType字符串參數(shù)類型
 paramType字符串參數(shù)請(qǐng)求方式:query/path
   query:對(duì)應(yīng)@RequestParam?傳遞
   path: 對(duì)應(yīng)@PathVariable{}path傳遞
 defaultValue字符串在api測試中默認(rèn)值
   用例參見項(xiàng)目中的設(shè)置
@ApiResponses{}@ApiResponse數(shù)組可用在方法頭上.參數(shù)的描述容器
   @ApiResponses({@ApiResponse1,@ApiResponse2,...})
@ApiResponsecode整形可用在@ApiResponses
 message字符串錯(cuò)誤描述
   @ApiResponse(code = 200, message = "Successful")

1.Demo結(jié)構(gòu)

Maven pom

<?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>com.reachauto.hkr.cxn</groupId>    <artifactId>cxn-demo-swagger2</artifactId>    <version>1.0-SNAPSHOT</version>    <parent>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-starter-parent</artifactId>        <version>Angel.SR6</version>    </parent>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-actuator</artifactId>        </dependency>        <!-- Swagger -->       <dependency>            <groupId>io.springfox</groupId>            <artifactId>springfox-swagger-ui</artifactId>            <version>2.2.2</version>        </dependency>        <dependency>            <groupId>io.springfox</groupId>            <artifactId>springfox-swagger2</artifactId>            <version>2.2.2</version>        </dependency>        <!-- END Swagger -->    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>

SwaggerConfig 配置

import com.google.common.base.Predicate;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.http.ResponseEntity;import springfox.documentation.service.ApiInfo;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;import static com.google.common.base.Predicates.or;import static springfox.documentation.builders.PathSelectors.regex;/** * SwaggerConfig */@Configuration@EnableSwagger2public class SwaggerConfig {    @Value("${server.servlet-path}")    private String pathMapping;    private ApiInfo initApiInfo() {        ApiInfo apiInfo = new ApiInfo("XXX項(xiàng)目 Platform API",//大標(biāo)題                initContextInfo(),//簡單的描述                "1.0.0",//版本                "服務(wù)條款",                "后臺(tái)開發(fā)團(tuán)隊(duì)",//作者                "The Apache License, Version 2.0",//鏈接顯示文字                "http://www.baidu.com"http://網(wǎng)站鏈接        );        return apiInfo;    }    private String initContextInfo() {        StringBuffer sb = new StringBuffer();        sb.append("REST API 設(shè)計(jì)在細(xì)節(jié)上有很多自己獨(dú)特的需要注意的技巧,并且對(duì)開發(fā)人員在構(gòu)架設(shè)計(jì)能力上比傳統(tǒng) API 有著更高的要求。")                .append("<br/>")                .append("本文通過翔實(shí)的敘述和一系列的范例,從整體結(jié)構(gòu),到局部細(xì)節(jié),分析和解讀了為了提高易用性和高效性,REST API 設(shè)計(jì)應(yīng)該注意哪些問題以及如何解決這些問題。");        return sb.toString();    }    @Bean    public Docket restfulApi() {        System.out.println("http://localhost:8080" + pathMapping + "/swagger-ui.html");        return new Docket(DocumentationType.SWAGGER_2)                .groupName("RestfulApi")//                .genericModelSubstitutes(DeferredResult.class)                .genericModelSubstitutes(ResponseEntity.class)                .useDefaultResponseMessages(true)                .forCodeGeneration(false)                .pathMapping(pathMapping) // base,最終調(diào)用接口后會(huì)和paths拼接在一起                .select()                .paths(doFilteringRules())                .build()                .apiInfo(initApiInfo());    }    /**     * 設(shè)置過濾規(guī)則     * 這里的過濾規(guī)則支持正則匹配     * @return     */    private Predicate<String> doFilteringRules() {        return or(                regex("/hello.*"),                regex("/vehicles.*")        );    }}

Controller 的配置

import com.reachauto.hkr.cxn.swagger.bean.ChangeRentalShopParameter;import io.swagger.annotations.*;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.*;import java.util.Date;/** * Created by cxn on 2016/2/24. */@Api(value = "API - VehiclesController", description = "車輛模塊接口詳情")@RestController@RequestMapping("/vehicles")public class VehiclesController {    private static Logger logger = LoggerFactory.getLogger(VehiclesController.class);    @ApiOperation(value = "查詢車輛接口", notes = "此接口描述xxxxxxxxxxxxx<br/>xxxxxxx<br>值得慶幸的是這兒支持html標(biāo)簽<hr/>", response = String.class)    @ApiImplicitParams({            @ApiImplicitParam(name = "vno", value = "車牌", required = false,                    dataType = "string", paramType = "query", defaultValue = "遼A12345"),            @ApiImplicitParam(name = "page", value = "page", required = false,                    dataType = "Integer", paramType = "query",defaultValue = "1"),            @ApiImplicitParam(name = "count", value = "count", required = false,                    dataType = "Integer", paramType = "query",defaultValue = "10")    })    @ApiResponses(value = {            @ApiResponse(code = 200, message = "Successful — 請(qǐng)求已完成"),            @ApiResponse(code = 400, message = "請(qǐng)求中有語法問題,或不能滿足請(qǐng)求"),            @ApiResponse(code = 401, message = "未授權(quán)客戶機(jī)訪問數(shù)據(jù)"),            @ApiResponse(code = 404, message = "服務(wù)器找不到給定的資源;文檔不存在"),            @ApiResponse(code = 500, message = "服務(wù)器不能完成請(qǐng)求")}    )    @ResponseBody    @RequestMapping(value = "", method = RequestMethod.GET)    public ModelMap findVehicles(@RequestParam(value = "vno", required = false) String vno,                                 @RequestParam(value = "page", required = false) Integer page,                                 @RequestParam(value = "count", required = false) Integer count)            throws Exception {        logger.info("http://localhost:8501/api/v1/vehicles");        logger.info("## {},{},{}", vno, page, count);        logger.info("## 請(qǐng)求時(shí)間:{}", new Date());        ModelMap map = new ModelMap();        map.addAttribute("vno", vno);        map.addAttribute("page", page);        return map;    }    @ApiOperation(value = "根據(jù)車牌查詢車輛", notes = "這種類型的查詢是精確查詢,其結(jié)果只有一條數(shù)據(jù)", response = String.class)    @ApiImplicitParams({            @ApiImplicitParam(name = "vno", value = "車牌", required = false,                    dataType = "string", paramType = "path", defaultValue = "遼A12345")    })    @ApiResponses(value = {            @ApiResponse(code = 200, message = "Successful — 請(qǐng)求已完成"),            @ApiResponse(code = 400, message = "請(qǐng)求中有語法問題,或不能滿足請(qǐng)求"),            @ApiResponse(code = 401, message = "未授權(quán)客戶機(jī)訪問數(shù)據(jù)"),            @ApiResponse(code = 404, message = "服務(wù)器找不到給定的資源;文檔不存在"),            @ApiResponse(code = 500, message = "服務(wù)器不能完成請(qǐng)求")}    )    @ResponseBody    @RequestMapping(value = "vno={vno}", method = RequestMethod.GET)    public ModelMap getVno(@PathVariable(value = "vno") String vno)            throws Exception {        logger.info("http://localhost:8501/api/v1/vehicles/vno={}", vno);        logger.info("## 請(qǐng)求時(shí)間:{}", new Date());        ModelMap map = new ModelMap();        map.addAttribute("vno", vno);        return map;    }    @ApiOperation(value = "車輛位置查詢接口", notes = "根據(jù)車牌查詢車輛位置信息", response = String.class)    @ApiImplicitParams({            @ApiImplicitParam(name = "vno", value = "車牌", required = false,                    dataType = "string", paramType = "path", defaultValue = "遼A12345")    })    @ApiResponses(value = {            @ApiResponse(code = 200, message = "Successful — 請(qǐng)求已完成"),            @ApiResponse(code = 400, message = "請(qǐng)求中有語法問題,或不能滿足請(qǐng)求"),            @ApiResponse(code = 401, message = "未授權(quán)客戶機(jī)訪問數(shù)據(jù)"),            @ApiResponse(code = 404, message = "服務(wù)器找不到給定的資源;文檔不存在"),            @ApiResponse(code = 500, message = "服務(wù)器不能完成請(qǐng)求")}    )    @ResponseBody    @RequestMapping(value = "vno={vno}/location", method = RequestMethod.GET)    public ModelMap getLocation(@PathVariable(value = "vno") String vno)            throws Exception {        logger.info("getLocation");        logger.info("## 請(qǐng)求時(shí)間:{}", new Date());        ModelMap map = new ModelMap();        map.addAttribute("vno", vno);        return map;    }    @ApiOperation(value = "根據(jù)車輛id查詢", notes = "精確查詢,最常規(guī)的方式,支持POST和GET方式", response = String.class)    @ApiImplicitParams({            @ApiImplicitParam(name = "id", value = "id", required = false,                    dataType = "string", paramType = "path", defaultValue = "12344444")    })    @ApiResponses(value = {            @ApiResponse(code = 200, message = "Successful — 請(qǐng)求已完成"),            @ApiResponse(code = 400, message = "請(qǐng)求中有語法問題,或不能滿足請(qǐng)求"),            @ApiResponse(code = 401, message = "未授權(quán)客戶機(jī)訪問數(shù)據(jù)"),            @ApiResponse(code = 404, message = "服務(wù)器找不到給定的資源;文檔不存在"),            @ApiResponse(code = 500, message = "服務(wù)器不能完成請(qǐng)求")}    )    @ResponseBody    @RequestMapping(value = "{id}", method = {RequestMethod.GET,RequestMethod.POST})    public ModelMap getById(@PathVariable(value = "id") String id)            throws Exception {        logger.info("http://localhost:8501/api/v1/vehicles/{}", id);        logger.info("## 請(qǐng)求時(shí)間:{}", new Date());        ModelMap map = new ModelMap();        map.addAttribute("{RequestMethod.GET,RequestMethod.POST}", id);        return map;    }    @ApiOperation(value = "根據(jù)車輛id查詢", notes = "精確查詢,最常規(guī)的方式,支持POST和GET方式", response = String.class)    @ApiImplicitParams({            @ApiImplicitParam(name = "id", value = "id", required = false,                    dataType = "string", paramType = "path", defaultValue = "12344444")    })    @ApiResponses(value = {            @ApiResponse(code = 200, message = "Successful — 請(qǐng)求已完成"),            @ApiResponse(code = 400, message = "請(qǐng)求中有語法問題,或不能滿足請(qǐng)求"),            @ApiResponse(code = 403, message = "服務(wù)器拒絕請(qǐng)求"),            @ApiResponse(code = 401, message = "未授權(quán)客戶機(jī)訪問數(shù)據(jù)"),            @ApiResponse(code = 404, message = "服務(wù)器找不到給定的資源;文檔不存在"),            @ApiResponse(code = 500, message = "服務(wù)器不能完成請(qǐng)求")}    )    @ResponseBody    @RequestMapping(value = "{id}", method = {RequestMethod.DELETE})    public ModelMap delById(@PathVariable(value = "id") String id)            throws Exception {        logger.info("http://localhost:8501/api/v1/vehicles/{}", id);        logger.info("## 請(qǐng)求時(shí)間:{}", new Date());        ModelMap map = new ModelMap();        map.addAttribute("RequestMethod.DELETE", id);        return map;    }    @ApiOperation(value = "網(wǎng)點(diǎn)掛靠", notes = "嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻", response = String.class)    @ApiResponses(value = {            @ApiResponse(code = 200, message = "Successful — 請(qǐng)求已完成"),            @ApiResponse(code = 400, message = "請(qǐng)求中有語法問題,或不能滿足請(qǐng)求"),            @ApiResponse(code = 401, message = "未授權(quán)客戶機(jī)訪問數(shù)據(jù)"),            @ApiResponse(code = 404, message = "服務(wù)器找不到給定的資源;文檔不存在"),            @ApiResponse(code = 500, message = "服務(wù)器不能完成請(qǐng)求")}    )    @ResponseBody    @RequestMapping(value = "change_rentalshop", method = {RequestMethod.PUT,RequestMethod.PATCH})    public ModelMap changeRentalShop(@RequestBody ChangeRentalShopParameter parameter)            throws Exception {        logger.info("http://localhost:8501/api/v1/vehicles/change_rentalshop | {}", parameter);        logger.info("## 請(qǐng)求時(shí)間:{}", new Date());        ModelMap map = new ModelMap();        map.addAttribute("網(wǎng)點(diǎn)掛靠", new Date());        return map;    }}

Application 啟動(dòng)模塊

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@EnableAutoConfiguration@ComponentScanpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}

2.演示地址

http://localhost:8080/api/v1/swagger-ui.html

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
SpringBoot非官方教程 | 第十一篇:springboot集成swagger2,構(gòu)建優(yōu)雅的Restful API
如何整合SpringMVC和Swagger2,并且使用Mock數(shù)據(jù)進(jìn)行聯(lián)調(diào)
dubbo實(shí)戰(zhàn)之二:與SpringBoot集成
jackson學(xué)習(xí)之九:springboot整合(配置文件)
Spring Boot中使用Swagger2構(gòu)建強(qiáng)大的RESTful API文檔
【精選】Swagger 3.0快速入門_swagger3
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服