SpringBoot之springfox(Swagger) (ApiDoc接口文档)

时间:2023-12-26 23:24:43

Springfox的前身是swagger-springmvc,是一个开源的API doc框架,可以将我们的Controller的方法以文档的形式展现,基于Swagger。

官网地址:http://springfox.github.io/springfox/

1.maven依赖

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

2.配置

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration
@EnableSwagger2
@SpringBootApplication
public class DemoApplication {     public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }     @Bean
    public Docket testApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("test")
                .genericModelSubstitutes(DeferredResult.class)
                //.genericModelSubstitutes(ResponseEntity.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(true)
                .pathMapping("/test")//api测试请求地址
                .select()
                .paths(PathSelectors.regex("/common/.*"))//过滤的接口
                .build()
                .apiInfo(testApiInfo());
    }     @Bean
    public Docket demoApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("demo")
                .genericModelSubstitutes(DeferredResult.class)
                //  .genericModelSubstitutes(ResponseEntity.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(false)
                .pathMapping("/")
                .select()
                .paths(PathSelectors.regex("/comm.*"))//过滤的接口
                .build()
                .apiInfo(demoApiInfo());
    }     private ApiInfo testApiInfo() {
        Contact contact = new Contact("王念", "http://my.oschina.net/wangnian", "2251181679@qq.com");
        ApiInfo apiInfo = new ApiInfo("某API接口",//大标题
                "REST风格API",//小标题
                "0.1",//版本
                "www.baidu.com",
                contact,//作者
                "主页",//链接显示文字
                ""//网站链接
        );
        return apiInfo;
    }     private ApiInfo demoApiInfo() {
        Contact contact = new Contact("王念", "http://my.oschina.net/wangnian", "2251181679@qq.com");
        ApiInfo apiInfo = new ApiInfo("某API接口",//大标题
                "REST风格API",//小标题
                "0.1",//版本
                "www.baidu.com",
                contact,//作者
                "主页",//链接显示文字
                ""//网站链接
        );
        return apiInfo;
    }
}

3.restful注解描述

package com.example.demo;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore; /**
 * Created by 王念 on 2016/4/25.
 */
@RestController
@RequestMapping("comm/")
public class Controller {     /**
     * 简单接口描述
     *
     * @param userName
     * @return
     */
    @RequestMapping(value = "/demo1", method = RequestMethod.POST)
    @ApiOperation(value = "测试接口1", notes = "简单接口描述 userName必填", code = 200, produces = "application/json")
    public ModelMap getDemo(@RequestParam("userName") String userName) {
        ModelMap map = new ModelMap();
        map.addAttribute("userName", userName);
        return map;
    }     /**
     * 参数描述
     *
     * @param userName
     * @return
     */
    @RequestMapping(value = "/demo2/{userName}", method = RequestMethod.POST)
    @ApiOperation(value = "测试接口2", notes = "参数描述", code = 200, produces = "application/json")
    public ModelMap getDemo2(@ApiParam(name = "userName", value = "编号", required = true) @PathVariable("userName") String userName) {
        ModelMap map = new ModelMap();
        map.addAttribute("userName", userName);
        return map;
    }     /**
     * 接受对象
     *
     * @return
     */
    @RequestMapping(value = "/demo3", method = RequestMethod.POST)
    @ApiOperation(value = "测试接口3", notes = "接受对象", code = 200, produces = "application/json")
    public ModelMap getDemoa(@RequestBody User user) {
        ModelMap map = new ModelMap();
        map.addAttribute("result", user);
        return map;
    }     @ApiIgnore //使用这个注解忽略这个接口
    @RequestMapping(value = "/demo4", method = RequestMethod.POST)
    public ModelMap getDemob(@RequestParam String content) {
        ModelMap map = new ModelMap();
        map.addAttribute("result", new java.util.Date());
        return map;
    }
}

访问http://localhost:8080/swagger-ui.html 就能看到

notes里 换行用  </br> HMLT的标签就行了,哈哈哈哈

http://my.oschina.net/wangnian/blog/666017