spring boot使用Swagger2

來源:互聯網
上載者:User

標籤:main   not   param   bsp   pac   demo1   boot   建立使用者   sed   

  • 1. 在pom.xml中加入Swagger2的依賴
 1 <!--加入Swagger2的依賴--> 2 <dependency> 3     <groupId>io.springfox</groupId> 4     <artifactId>springfox-swagger2</artifactId> 5     <version>2.2.2</version> 6 </dependency> 7 <dependency> 8     <groupId>io.springfox</groupId> 9     <artifactId>springfox-swagger-ui</artifactId>10     <version>2.2.2</version>11 </dependency>                    
  • 2. 在Application.java同級建立Swagger2的配置類Swagger2
 1 import org.springframework.context.annotation.Bean; 2 import org.springframework.context.annotation.Configuration; 3 import springfox.documentation.builders.ApiInfoBuilder; 4 import springfox.documentation.builders.PathSelectors; 5 import springfox.documentation.builders.RequestHandlerSelectors; 6 import springfox.documentation.service.ApiInfo; 7 import springfox.documentation.spi.DocumentationType; 8 import springfox.documentation.spring.web.plugins.Docket; 9 import springfox.documentation.swagger2.annotations.EnableSwagger2;10 11 12 @Configuration //讓Spring來載入該類配置13 @EnableSwagger2 //啟用Swagger214 public class Swagger2 {15 16     @Bean17     public Docket createRestApi() {18         return new Docket(DocumentationType.SWAGGER_2)19                 .apiInfo(apiInfo())//用來建立該Api的基本資料(這些基本資料會展現在文檔頁面中)20                 .select()21                 //用來控制哪些介面暴露給Swagger來展現,22                 //本例採用指定掃描的包路徑來定義,Swagger會掃描該包下所有Controller定義的API,併產生文檔內容23                 //(除了被@ApiIgnore指定的請求)24                 .apis(RequestHandlerSelectors.basePackage("com.example.demo1.web"))25                 .paths(PathSelectors.any())26                 .build();27     }28 29     private ApiInfo apiInfo() {30         return new ApiInfoBuilder()31                 .title("Spring Boot中使用Swagger2構建RESTful APIs")32                 .description("更多Spring Boot相關文章請關註:https://www.cnblogs.com/yangjiming/category/1240129.html")33                 .termsOfServiceUrl("http://www.cnblogs.com/yangjiming/")34                 .contact("Mr.yang")35                 .version("1.0")36                 .build();37     }38 }
  • 3.  在代碼中添加文檔內容,通過@ApiOperation註解來給API增加說明、通過@ApiImplicitParams、@ApiImplicitParam註解來給參數增加說明。
 1 import com.example.demo1.domain.User; 2 import io.swagger.annotations.ApiImplicitParam; 3 import io.swagger.annotations.ApiImplicitParams; 4 import io.swagger.annotations.ApiOperation; 5 import org.springframework.web.bind.annotation.*; 6  7 import java.util.*; 8  9 @RestController10 @RequestMapping(value="/users")     // 通過這裡配置使下面的映射都在/users下11 public class UserController {12 13     static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());14 15     @ApiOperation(value="擷取使用者列表", notes="")16     @RequestMapping(value={""}, method=RequestMethod.GET)17     public List<User> getUserList() {18         List<User> r = new ArrayList<User>(users.values());19         return r;20     }21 22     @ApiOperation(value="建立使用者", notes="根據User對象建立使用者")23     @ApiImplicitParam(name = "user", value = "使用者詳細實體user", required = true, dataType = "User")24     @RequestMapping(value="", method=RequestMethod.POST)25     public String postUser(@RequestBody User user) {26         users.put(user.getId(), user);27         return "success";28     }29 30     @ApiOperation(value="擷取使用者詳細資料", notes="根據url的id來擷取使用者詳細資料")31     @ApiImplicitParam(name = "id", value = "使用者ID", required = true, dataType = "Long", paramType="path")32     @RequestMapping(value="/{id}", method=RequestMethod.GET)33     public User getUser(@PathVariable Long id) {34         return users.get(id);35     }36 37     @ApiOperation(value="更新使用者詳細資料", notes="根據url的id來指定更新對象,並根據傳過來的user資訊來更新使用者詳細資料")38     @ApiImplicitParams({39             @ApiImplicitParam(name = "id", value = "使用者ID", required = true, dataType = "Long", paramType="path"),40             @ApiImplicitParam(name = "user", value = "使用者詳細實體user", required = true, dataType = "User")41     })42     @RequestMapping(value="/{id}", method=RequestMethod.PUT)43     public String putUser(@PathVariable Long id, @RequestBody User user) {44         User u = users.get(id);45         u.setName(user.getName());46         u.setAge(user.getAge());47         users.put(id, u);48         return "success";49     }50 51     @ApiOperation(value="刪除使用者", notes="根據url的id來指定刪除對象")52     @ApiImplicitParam(name = "id", value = "使用者ID", required = true, dataType = "Long" , paramType="path")53     @RequestMapping(value="/{id}", method=RequestMethod.DELETE)54     public String deleteUser(@PathVariable Long id) {55         users.remove(id);56         return "success";57     }58 }
 1 public class User { 2  3     private Long id; 4     private String name; 5     private Integer age; 6  7  8     public Long getId() { 9         return id;10     }11 12     public String getName() {13         return name;14     }15 16     public Integer getAge() {17         return age;18     }19 20     public void setId(Long id) {21         this.id = id;22     }23 24     public void setName(String name) {25         this.name = name;26     }27 28     public void setAge(Integer age) {29         this.age = age;30     }31 }
View Code
  • 4. 完成上述代碼添加上,啟動Spring Boot程式,訪問:http://localhost:8080/swagger-ui.html
  • 原文參考地址:http://blog.didispace.com/springbootswagger2/

spring boot使用Swagger2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.