在 spring mvc 中使用 swagger2
来源:互联网 发布:公交线路优化调整方案 编辑:程序博客网 时间:2024/05/22 12:41
在 spring mvc 中使用 swagger2
第一步:配置 prom.xml 文件
<!-- swagger-springmvc --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.5.0</version> </dependency> <!-- swagger-springmvc dependencies -->
第二部配置: spring-mvc.xml 文件
<mvc:annotation-driven /> <!-- enable autowire 向容器自动注册 --> <context:annotation-config /> <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 --> <context:component-scan base-package="cn.com.xxxxx.controller" /> <!-- 使用 Swagger Restful API文档时,添加此注解 --> <mvc:default-servlet-handler /> <!-- swagge 访问路径 --> <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" /> <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
第三部创建SwaggerConfig.class
package cn.com.biyouxiu.tools;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;import io.swagger.annotations.ApiOperation;import io.swagger.annotations.ApiParam;import springfox.documentation.builders.ApiInfoBuilder;import springfox.documentation.builders.PathSelectors;import springfox.documentation.builders.RequestHandlerSelectors;import springfox.documentation.service.ApiInfo;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;/** * @author lpf * @version v1.0:2017年8月8日 上午10:10:06 * swagger配置文件 */@EnableWebMvc @EnableSwagger2 @Configurationpublic class SwaggerConfig extends WebMvcConfigurationSupport{ @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("biyouxiu对外开放平台") .description("HTTP对外开放接口") .version("1.0.0") .termsOfServiceUrl("http://xxx.xxx.com") .license("所有学生端非注册操作都需要 token否则非法") .licenseUrl("http://xxx.xxx.com") .build(); }}
第四部:对 controller 进行注释
@RequestMapping(value = "showCourseList", method = RequestMethod.GET) @ResponseBody @ApiOperation(value="获取课程首页信息",httpMethod="GET") public Map<String, java.lang.Object> getCourseList(@ApiParam(value="token",required=true)HttpServletRequest request) { return courseService.getCourseList((String) request.getAttribute("userId")); }
第五步:访问 localhost:8080//项目名/swagger-ui.html
大功告成
阅读全文
0 0
- 在 spring mvc 中使用 swagger2
- 在spring-mvc中使用swagger2遇到的问题汇总
- Spring MVC中使用 Swagger2 构建Restful API
- Spring MVC中使用 Swagger2 构建Restful API
- 【实践笔记】Spring MVC中Restful API使用 Swagger2 构建
- Spring MVC中使用 Swagger2 构建Restful API
- Spring MVC中使用 Swagger2 构建Restful API
- Spring MVC中使用 Swagger2 构建Restful API
- Spring MVC中快速集成swagger2
- Spring MVC 整合 Swagger2
- spring mvc 4 + swagger2
- 在spring boot项目中应用swagger2
- spring mvc 4.2 集成 swagger2
- Swagger2与Spring mvc集成
- 在spring-boot中使用swagger2来构建RESTful API文档
- Spring Boot使用Swagger2
- spring boot 使用Swagger2
- dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb3.4-redis3(十)之Spring MVC中使用 Swagger2 构建Restful API
- python学习笔记之001.py
- Let the Balloon Rise
- 用java实现一颗平衡二叉树ADT
- 装饰器模式
- TCP并发服务器,每个客户一个子进程
- 在 spring mvc 中使用 swagger2
- 用户模式下的线程同步——Slim读/写锁
- JQuery的Ajax跨域请求的解决方案
- javascript写打地鼠
- POJ2139-Six Degrees of Cowvin Bacon
- 多线程(4)—同步方法
- Hdu 6082 度度熊与邪恶大魔王 背包DP
- 有关Ajax套用问题,表格列表等宽属性,Mybatis从数据库读取数据。
- 游戏对象未激活时,无法调用awake() start()