SpringBoot整合Swagger自動產生API文檔

來源:互聯網
上載者:User

swagger用於定義API文檔。

好處: 前後端分離開發 API文檔非常明確 測試的時候不需要再使用URL輸入瀏覽器的方式來訪問Controller 傳統的輸入URL的測試方式對於post請求的傳參比較麻煩(當然,可以使用postman這樣的瀏覽器外掛程式) spring-boot與swagger的整合簡單的一逼

首先,在項目pom中引入依賴,如下,

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

接著,在SpringBoot中建立Application.java,如下,

package com.xxx.firstboot;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import springfox.documentation.swagger2.annotations.EnableSwagger2;@SpringBootApplication        //same as @Configuration+@EnableAutoConfiguration+@ComponentScan@EnableSwagger2             //啟動swagger註解public class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}

說明: 引入了一個註解@EnableSwagger2來啟動swagger註解。(啟動該註解使得用在controller中的swagger註解生效,覆蓋的範圍由@ComponentScan的配置來指定,這裡預設指定為根路徑”com.xxx.firstboot”下的所有controller)

接著,建立UserController.java,如下,

package com.xxx.firstboot.web;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestHeader;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import com.xxx.firstboot.domain.User;import com.xxx.firstboot.service.UserService;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;import io.swagger.annotations.ApiResponses;@RestController@RequestMapping("/user")@Api("userController相關api")public class UserController {    @Autowired    private UserService userService;//    @Autowired//    private MyRedisTemplate myRedisTemplate;    @ApiOperation("擷取使用者資訊")    @ApiImplicitParams({        @ApiImplicitParam(paramType="header",name="username",dataType="String",required=true,value="使用者的姓名",defaultValue="zhaojigang"),        @ApiImplicitParam(paramType="query",name="password",dataType="String",required=true,value="使用者的密碼",defaultValue="wangna")    })    @ApiResponses({        @ApiResponse(code=400,message="請求參數沒填好"),        @ApiResponse(code=404,message="請求路徑沒有或頁面跳轉路徑不對")    })    @RequestMapping(value="/getUser",method=RequestMethod.GET)    public User getUser(@RequestHeader("username") String username, @RequestParam("password") String password) {        return userService.getUser(username,password);    }//    @RequestMapping("/testJedisCluster")//    public User testJedisCluster(@RequestParam("username") String username){//        String value =  myRedisTemplate.get(MyConstants.USER_FORWARD_CACHE_PREFIX, username);//        if(StringUtils.isBlank(value)){//            myRedisTemplate.set(MyConstants.USER_FORWARD_CACHE_PREFIX, username, JSON.toJSONString(getUser()));//            return null;//        }//        return JSON.parseObject(value, User.class);//    }}

說明:

具體其他的註解,查看:

https://github.com/swagger-api/swagger-core/wiki/Annotations#apimodel

接著,在瀏覽器輸入”http://localhost:8080/swagger-ui.html“,可以看到如下介面,

最上邊一個紅框:@Api

GET紅框:method=RequestMethod.GET

右邊紅框:@ApiOperation

parameter紅框:@ApiImplicitParams系列註解

response messages紅框:@ApiResponses系列註解

輸入參數後,點擊”try it out!”,查看響應內容:

其他參考:

==http://www.cnblogs.com/java-zhao/p/5348113.html==

https://github.com/swagger-api/swagger-core/wiki/Annotations#apimodel

http://www.jianshu.com/p/8033ef83a8ed

http://www.cnblogs.com/softidea/p/6251249.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.