SpringBoot之springfox(Swagger) spring-boot-starter-swagger

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

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

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

2019/3/21更新:最新用程序员DD的springboot兼容包,很方便零配置.

https://github.com/SpringForAll/spring-boot-starter-swagger

1.maven依赖



   io.springfox
   springfox-swagger2
   2.4.0


   io.springfox
   springfox-swagger-ui
   2.4.0

 

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", "[email protected]");
        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", "[email protected]");
        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里 换行用  
HMLT的标签就行了,哈哈哈哈

最后学一下别人,爬虫爬完这篇文章没有标注转载

 

来源:http://my.oschina.net/wangnian/blog/666017  嘻嘻

 

 

 

转载于:https://my.oschina.net/wangnian/blog/666017

你可能感兴趣的:(SpringBoot之springfox(Swagger) spring-boot-starter-swagger)