當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Springboot整合swagger指南
生活随笔
收集整理的這篇文章主要介紹了
Springboot整合swagger指南
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Springboot整合swagger指南
1. 安裝使用
1.1 下載依賴
<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>不同版本,配置有所差異,最好采用相同版本
1.2 編寫swagger配置文件
@Configuration @EnableSwagger2 public class SwaggerConfig {@Beanpublic Docket docket() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(myApiInfo());}private ApiInfo myApiInfo() {Contact DEFAULT_CONTACT = new Contact("夏2同學", "http://appletest.cn", "1754082565@qq.com");return new ApiInfo("Api Documentation","Api Documentation description...","1.0","urn:tos",DEFAULT_CONTACT,"Apache 2.0","http://www.apache.org/licenses/LICENSE-2.0",new ArrayList());}}1.3 啟動使用
對,springboot集成swagger,就是這么簡單!
啟動后,就可以看到我們的swagger界面。
它默認會掃描,我們項目下面的所有controller和bean層,幫助我們直接生成文檔。
controller部分代碼如下:
@RestController @Api(description = "hello工程") public class HelloController {@GetMapping("/hello")@ApiOperation("hello的Api")public String hello(@ApiParam("用戶名") String username){return "hello " + username;}@PostMapping("/post")public String post(String username, @RequestParam String password){return "hello " + username + ", " + password;}}2. 注解使用
通過使用注解,可以讓swagger上顯示的信息更加語義化,減少溝通的成本。
2.1 用于注解pojo的兩個:
@ApiModel(description = Content類描述) public class Content { @ApiModelProperty(標題) private String title;實例:
@Data @AllArgsConstructor @NoArgsConstructor @ApiModel("user實體類") public class User {@ApiModelProperty("用戶名")private String username;@ApiModelProperty("密碼")private String password;@ApiModelProperty("生日")private Date birthDate; }swagger運行效果:
2.2 用于控制器類的3個
@Api(tags = {Index控制器}) // tags是對Controller的接口重新分類 @Controller public class IndexController {@ApiOperation(getContent方法)@GetMapping("/test")@ResponseBodypublic Content getContent(@ApiParam(名字) @RequestParam String name) { 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Springboot整合swagger指南的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux服务器配置PHP文件下载,出现
- 下一篇: springboot下使用mybatis