SpringBoot中使用Swagger

Swagger是一個(gè)用于構(gòu)建生成Restful API文檔的開源框架腾供,主要包括以下工具集:

  • Swagger Core Java相關(guān)類庫油湖,生成以及處理Swagger的定義規(guī)范
  • Swagger Editor 使用YAMl格式編寫Swagger腳本
  • Swagger Codegen 針對各種語言的SDK劳翰,用于生成文檔
  • Swagger UI 基于HTML5的可交互的UI

Springfox是基于Swagger的Api文檔生成工具航棱,對SpringMvc支持很好弥臼,在Springboot中可以使用Springfox生成在線Api文檔

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.0</version>
</dependency>

配置類速种,指定掃描路徑以及顯示內(nèi)容

@Configuration
@EnableSwagger2
@Profile("dev")
//@ComponentScan
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                    .paths(PathSelectors.any())
                .build()
                .globalResponseMessage(RequestMethod.GET,
                        Lists.newArrayList(new ResponseMessageBuilder().code(500).message("500 ERROR").build()));
    }

    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("用戶管理系統(tǒng)")
                .description("Restful API文檔")
                .contact(new Contact("admin","http://xxx.zz.com", "admin@email.com"))
                .version("1.0")
                .build();

        return apiInfo;
    }
}

在Controller中使用Swagger注解

@Api(value="用戶管理",tags = {"用戶管理API"},description = "描述信息")
@Controller
public class UserController {

    @ApiOperation(value = "添加用戶", notes = "添加用戶notes", produces = "application/json")
    @ResponseBody
    @RequestMapping(value= "/user", method = RequestMethod.POST)
    public UserResult user(@ApiParam(value="用戶參數(shù)") @RequestBody UserParam param) {
        UserResult userResult = new UserResult();
        userResult.setId("Hello" + param.getName());
        userResult.setAge(param.getAge());
        return userResult;
    }

    @ApiOperation("查詢用戶")
    @ApiImplicitParams({
        @ApiImplicitParam(name="name",value="用戶名",dataType = "string",paramType = "query")
    })
    @ResponseBody
    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public UserResult user(HttpServletRequest req) {
        String name = req.getParameter("name");
        UserResult userResult = new UserResult();
        userResult.setId("Hello" + name);
        userResult.setAge(100);
        return userResult;
    }

    @ApiOperation("獲取用戶")
    @ResponseBody
    @RequestMapping(value= "/user/{name}", method = RequestMethod.GET)
    @ApiResponses(value={
            @ApiResponse(code=123,message="xxx"),
            @ApiResponse(code=405,message="not found")
    })
    public UserResult user(@ApiParam(value = "用戶名") @PathVariable("name") String name, @RequestParam("query") String query) {
        UserResult userResult = new UserResult();
        userResult.setId("Hello" + name);
        userResult.setAge(100);
        return userResult;
    }
}

請求參數(shù)實(shí)體,可使用ApiModel以及ApiModelProperty

@ApiModel(value="用戶參數(shù)", description = "用戶參數(shù)描述")
public class UserParam {

    @ApiModelProperty(value="用戶名", required = true)
    private String name;
    private Integer age;
    
     ...// set get
}

返回內(nèi)容對象

@ApiModel
public class UserResult {

    @ApiModelProperty(name="yyy",value="xxx")
    private String id;
    private Integer age;
    
    ... 
}

訪問Swagger Api: http://localhost:8080/swagger-ui.html



Swagger常用注解:

  • @Api 標(biāo)識(shí)Controller使用swagger.
  • @ApiImplicitParam 表示一個(gè)隱式的請求參數(shù)哭当,即請求方法中沒有顯示綁定參數(shù)名稱.
  • @ApiImplicitParams 表示隱式參數(shù)列表.
  • @ApiModel 描述請求或返回對象的額外信息.
  • @ApiModelProperty 描述或者生成ApiModel的成員變量.
  • @ApiOperation 描述一個(gè)Http請求方法
  • @ApiParam 描述顯示的請求參數(shù).
  • @ApiResponse 描述可能的返回對象.
  • @ApiResponses 表示返回對象列表.
  • @Authorization 申明認(rèn)證信息.
  • @ResponseHeader 表示返回頭部信息.

相關(guān)文檔:

  1. http://springfox.github.io/springfox/docs/current/#springfox-samples
  2. https://github.com/swagger-api/swagger-core/wiki/Annotations-1.5.X#api
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末猪腕,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子钦勘,更是在濱河造成了極大的恐慌陋葡,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件彻采,死亡現(xiàn)場離奇詭異腐缤,居然都是意外死亡捌归,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進(jìn)店門岭粤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來惜索,“玉大人,你說我怎么就攤上這事剃浇〗碚祝” “怎么了?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵虎囚,是天一觀的道長臼寄。 經(jīng)常有香客問我,道長溜宽,這世上最難降的妖魔是什么吉拳? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮适揉,結(jié)果婚禮上留攒,老公的妹妹穿的比我還像新娘。我一直安慰自己嫉嘀,他們只是感情好炼邀,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著剪侮,像睡著了一般拭宁。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上瓣俯,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天杰标,我揣著相機(jī)與錄音,去河邊找鬼彩匕。 笑死腔剂,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的驼仪。 我是一名探鬼主播掸犬,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼绪爸!你這毒婦竟也來了湾碎?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤奠货,失蹤者是張志新(化名)和其女友劉穎介褥,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡呻顽,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年雹顺,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了丹墨。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片廊遍。...
    茶點(diǎn)故事閱讀 40,115評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖贩挣,靈堂內(nèi)的尸體忽然破棺而出喉前,到底是詐尸還是另有隱情,我是刑警寧澤王财,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布卵迂,位于F島的核電站,受9級特大地震影響绒净,放射性物質(zhì)發(fā)生泄漏见咒。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一挂疆、第九天 我趴在偏房一處隱蔽的房頂上張望改览。 院中可真熱鬧,春花似錦缤言、人聲如沸宝当。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽庆揩。三九已至,卻和暖如春跌穗,著一層夾襖步出監(jiān)牢的瞬間订晌,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工蚌吸, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留腾仅,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓套利,卻偏偏與公主長得像推励,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子肉迫,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Swashbuckle.AspNetCore Swagger 使用Asp.net core 建立API.生成漂亮的...
    TooYummyToThrow閱讀 5,180評論 0 1
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,180評論 25 707
  • 首先需要導(dǎo)入Swagger依賴<dependency> <groupId>io.springfox</groupI...
    RageYourDream_閱讀 901評論 0 0
  • 能同途偶遇在這顆星球上喊衫,燃亮飄渺人生跌造,我多么夠運(yùn)。 十五歲的葉菲木訥,寡言壳贪,孤僻陵珍,小個(gè)子,皮膚黑违施,唯一的亮點(diǎn)就是有...
    依米在312閱讀 1,724評論 39 34
  • 嚴(yán)格地說互纯,判斷一個(gè)人是否年輕的標(biāo)志只有一條,就是看這個(gè)人還能否尋找刺激磕蒲,人和人略有不同留潦,感受到的刺激也不同,有的人...
    彭先生10閱讀 269評論 1 2