首页 » 文章 » spring boot引入swagger

spring boot 引入swagger可以方便的测试api和查看api文档
1.引入依赖
 <!-- Swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
        </dependency>
 
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>

2.加入SwaggerConfig.java

package tv.rr.panther.base.config;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import tv.rr.panther.base.util.PropertiesUtil;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * @title
 * @description
 * @usage

 * @author shenfeng
 * @create 2018/2/12 10:14
 */
 
@Profile({"dev","qtest"})
 
@Configuration
@EnableSwagger2
@ComponentScan
@EnableWebMvc
public class SwaggerConfig {
 
    private static final String SWAGGER_FLAG = PropertiesUtil.getPropertyValue("application", "swagger.enable");
 
    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .enable(true)
                .globalOperationParameters(getGlobalHeaderParameters())
                .apiInfo(apiInfo());
    }
 
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("拍客")
                .description("paike接口文档")
                .version("1.0.0")
                .build();
    }
 
    private List<Parameter> getGlobalHeaderParameters() {
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> parameters = new ArrayList<>(3);
        parameterBuilder.name("appId").description("客户端version_Platform")
                .modelRef(new ModelRef("string"))
                .parameterType("header").required(true).defaultValue("3.7.0_ios");
 
        parameterBuilder.name("token").description("客户端version_Platform")
                .modelRef(new ModelRef("string"))
                .parameterType("header").required(true).defaultValue("a261fdc8ddab4e8b8b233d9d011da442");
 
        parameters.add(parameterBuilder.build());
        parameterBuilder.name("sign").description("客户端签名md5(timestamp+token+appId)")
                .modelRef(new ModelRef("string"))
                .parameterType("header").required(true).defaultValue("md5(12132345645_token_3.7.2_ios)");
        parameters.add(parameterBuilder.build());
        return parameters;
    }
 
}

3.在controller上贴注解吧

package tv.rr.panther.controller;
 
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tv.rr.panther.base.controller.BaseController;
import tv.rr.panther.base.view.CommonResult;
import tv.rr.panther.responseData.tag.SelectTagListResponseData;
import tv.rr.panther.responseData.tag.TagListResponseData;
import tv.rr.panther.service.TagService;
import tv.rr.panther.view.TagView;
import tv.rr.panther.view.TagViewNoPic;
 
import java.util.List;
 
/**
 * @title
 * @description
 * @usage
 * @author shenfeng
 * @create 4/28/2018 3:36 PM
 *
 *                             _ooOoo_
 *                            o8888888o
 *                            88" . "88
 *                            (| -_- |)
 *                            O\  =  /O
 *                         ____/`---'\____
 *                       .'  \\|     |//  `.
 *                      /  \\|||  :  |||//  \
 *                     /  _||||| -:- |||||-  \
 *                     |   | \\\  -  /// |   |
 *                     | \_|  ''\---/''  |   |
 *                     \  .-\__  `-`  ___/-. /
 *                   ___`. .'  /--.--\  `. . __
 *                ."" '<  `.___\_<|>_/___.'  >'"".
 *               | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *               \  \ `-.   \_ __\ /__ _/   .-` /  /
 *          ======`-.____`-.___\_____/___.-`____.-'======
 *                             `=---='
 *          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *                     佛祖保佑        永无BUG
 */
@RequestMapping("tag")
@Api
@Controller
public class TagController extends BaseController {
 
    @Autowired
    private TagService tagService;
 
    @RequestMapping("recommendList")
    @ApiOperation(value = "首页推荐标签列表",httpMethod = "GET")
    @ResponseBody
    public CommonResult<TagListResponseData> recommondList(){
        CommonResult<TagListResponseData> result = CommonResult.getInstance();
        TagListResponseData tagListResponseData = new TagListResponseData();
        List<TagView> list = tagService.recommendList();
        tagListResponseData.setTagList(list);
        result.setData(tagListResponseData);
        return result;
    }
 
    /**
     * 可选列表,目前是所有的
     * @return
     */
    @RequestMapping("selectList")
    @ApiOperation(value = "视频可选标签",httpMethod = "GET")
    @ResponseBody
    public CommonResult<SelectTagListResponseData> selectList(){
        CommonResult<SelectTagListResponseData> result = CommonResult.getInstance();
 
        SelectTagListResponseData selectTagListResponseData = new SelectTagListResponseData();
 
        List<TagViewNoPic> list = tagService.allList();
        selectTagListResponseData.setTagList(list);
        result.setData(selectTagListResponseData);
 
        return result;
    }
 
}
  1. WebAppConfig 中web mvc注册路径
package tv.rr.panther.base.config;
 
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
 
/**
 * @title
 * @description
 * @usage
 * @author shenfeng
 * @create 2018/2/12 10:19
 */
@Profile({"dev","qtest"})
@Configuration
@EnableWebMvc
public class WebAppConfig extends WebMvcConfigurerAdapter {
 
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
 
}

添加新评论