这篇文章上次修改于 1748 天前,可能其部分内容已经发生变化,如有疑问可询问作者。 >spring boot 引入swagger可以方便的测试api和查看api文档 1.引入依赖 ```xml io.springfox springfox-swagger-ui 2.8.0 io.springfox springfox-swagger2 2.8.0 ``` 2.加入SwaggerConfig.java ```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 getGlobalHeaderParameters() { ParameterBuilder parameterBuilder = new ParameterBuilder(); List 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上贴注解吧 ```java 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 recommondList(){ CommonResult result = CommonResult.getInstance(); TagListResponseData tagListResponseData = new TagListResponseData(); List list = tagService.recommendList(); tagListResponseData.setTagList(list); result.setData(tagListResponseData); return result; } /** * 可选列表,目前是所有的 * @return */ @RequestMapping("selectList") @ApiOperation(value = "视频可选标签",httpMethod = "GET") @ResponseBody public CommonResult selectList(){ CommonResult result = CommonResult.getInstance(); SelectTagListResponseData selectTagListResponseData = new SelectTagListResponseData(); List list = tagService.allList(); selectTagListResponseData.setTagList(list); result.setData(selectTagListResponseData); return result; } } ``` 4. WebAppConfig 中web mvc注册路径 ```java 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/"); } } ```
没有评论