swagger2.0与spring结合
- 怎么使用呢 这里慢慢道来,我们一般用的都是maven工程,所以这里直接上maven依赖
<swagger.version>1.5.8</swagger.version>
<io.springfox.version>2.5.0</io.springfox.version>
<!-- swagger start -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-core</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${io.springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${io.springfox.version}</version>
</dependency>
<!-- swagger end -->
- 这里我们需要注入SwaggerConfiguration配置,直接上代码
/**
* Created by max on 8/16/16.
*
*/
@EnableSwagger2
public class SwaggerConfiguration { @Bean
public Docket getApiInfo() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("outer api")
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(outApiInfo()); } private ApiInfo outApiInfo() {
return new ApiInfo(
"mylearn 前后端接口-外部", // title 标题
"外部接口文档", // description 描述 标题下
"1.0.0", // version
"http://mylearn/*", // termsOfService
new Contact("xieyuebin","","xieyuebin@meituan.com"), // contact
"Apache 2.0", // licence
"http://www.apache.org/licenses/LICENSE-2.0.html" // licence url
); } @Bean
public UiConfiguration getUiConfig() {
return new UiConfiguration(
null,// url,暂不用
"none", // docExpansion => none | list
"alpha", // apiSorter => alpha
"schema", // defaultModelRendering => schema
UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS,
false, // enableJsonEditor => true | false
true); // showRequestHeaders => true | false
}
- 然后我们还需要把swagger和spring mvc结合起来,首先在web.xml里加入swagger文档的访问路径:
<!-- swagger ui 的静态资源交由default处理-->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/swagger-ui.html</url-pattern>
</servlet-mapping>
- 然后我们在dispatch-servlet.xml里加入对swagger静态资源jar包的访问路径如下:
<!-- swagger 配置 ,线上版本需要注释掉 -->
<beans:bean class="com.meituan.maxtse.mylearn.swagger.SwaggerConfiguration"/>
<!-- swagger ui resources-->
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars"/>
- 然后我们在controller和请求参数和返回参数里加入swagger提供的注解,直接上例子
/**
* Created by max on 10/11/16.
*/
@ApiModel(description = "用户请求表单")
public class UserForm { @ApiModelProperty(value = "姓名", example = "maxTse",position = 1)
private String username; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} @Override
public String toString() {
return "UserForm{" +
"username='" + username + '\'' +
'}';
}
}
@JsonInclude(JsonInclude.Include.NON_NULL)
@ApiModel
public class OutResult<T> implements Serializable { @ApiModelProperty(value = "数据", example = "")
public T data; @ApiModelProperty(value = "状态码,0表示成功 其他表示失败", example = "0")
public int status; @ApiModelProperty(value = "错误信息", example = "操作成功")
public String message = "";
/**
* Created by max on 8/16/16.
*/
@Controller
@RequestMapping(value = "/test", consumes = "application/json", produces = "application/json")
public class TestController { private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class); @ApiOperation(value = "swagger test", notes = "swagger test first", tags = {SwaggerConstants.TEST_TAG})
@ResponseBody
@RequestMapping(value = "/first", method = RequestMethod.POST)
public OutResult<String> first(@RequestBody UserForm userForm) {
LOGGER.info("first userForm={}", userForm);
throw new RuntimeException("dd");
/*
return OutResult.successResult(userForm.getUsername());
*/
}
- 下面我们启动项目,然后通过url:localhost:8080/swagger-ui.html来访问,就可以看到swagger文档界面了
swagger2.0与spring结合的更多相关文章
- Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(二)
在上一篇文章中我详细的介绍了如何搭建maven环境以及生成一个maven骨架的web项目,那么这章中我将讲述Spring MVC的流程结构,Spring MVC与Struts2的区别,以及例子中的一些 ...
- Tomcat 6.0.32 +Spring dbcp datasource关闭Tomcat出现严重异常
异常如下: 信息: Pausing Coyote HTTP/ -- :: org.apache.catalina.core.StandardService stop 信息: Stopping serv ...
- spring boot 2.0.3+spring cloud (Finchley)3、声明式调用Feign
Feign受Retrofix.JAXRS-2.0和WebSocket影响,采用了声明式API接口的风格,将Java Http客户端绑定到他的内部.Feign的首要目标是将Java Http客户端调用过 ...
- spring boot 2.0.3+spring cloud (Finchley)2、搭建负载均衡Ribbon (Eureka+Ribbon+RestTemplate)
Ribbon是Netflix公司开源的一个负载均衡组件,将负载均衡逻辑封装在客户端中,运行在客户端的进程里. 本例子是在搭建好eureka的基础上进行的,可参考spring boot 2.0.3+sp ...
- spring boot 2.0.3+spring cloud (Finchley)8、微服务监控Spring Boot Admin
参考:Spring Boot Admin 2.0 上手 Spring Boot Admin 用于管理和监控一个或多个Spring Boot程序,在 Spring Boot Actuator 的基础上提 ...
- spring boot 2.0.3+spring cloud (Finchley)5、路由网关Spring Cloud Zuul
Zuul作为微服务系统的网关组件,用于构建边界服务,致力于动态路由.过滤.监控.弹性伸缩和安全. 为什么需要Zuul Zuul.Ribbon以及Eureka结合可以实现智能路由和负载均衡的功能:网关将 ...
- spring boot 2.0.3+spring cloud (Finchley)4、熔断器Hystrix
在分布式系统中服务与服务之间的依赖错综复杂,一种不可避免的情况就是某些服务会出现故障,导致依赖于他们的其他服务出现远程调度的线程阻塞.某个服务的单个点的请求故障会导致用户的请求处于阻塞状态,最终的结果 ...
- 【Rocket MQ】RocketMQ4.2.0 和 spring boot的结合使用,实现分布式事务
RocketMQ4.2.0 和 spring boot的结合使用,实现分布式事务 参考地址:https://www.jianshu.com/p/f57de40621a0
- 0、Spring 注解驱动开发
0.Spring注解驱动开发 0.1 简介 <Spring注解驱动开发>是一套帮助我们深入了解Spring原理机制的教程: 现今SpringBoot.SpringCloud技术非常火热,作 ...
随机推荐
- DataX简介
DataX 是阿里巴巴集团内被广泛使用的离线数据同步工具/平台,实现包括 MySQL.Oracle.SqlServer.Postgre.HDFS.Hive.ADS.HBase.TableStore(O ...
- elasticsearch 嵌套对象之嵌套类型
nested类型是一种特殊的对象object数据类型(specialised version of the object datatype ),允许对象数组彼此独立地进行索引和查询. 1. 对象数组如 ...
- 四、附加到进程调试(.NET Framework)
附加到进程调试: 1.需要在IIS配置环境并可运行即通过浏览器可打开. 2.找到项目w3wp.exe进程并附加到进程调试,点击项目添加断点,直接访问浏览器即可. 优点:w3wp.exe是已经运行的,调 ...
- linuxprobe培训第3节课笔记2019年7月7日
linux常用命令: echo:在终端输出字符串或变量提取后的值 date:显示及设置系统的时间或日期 reboot:重启 poweroff:关机 wget:下载 ps:查看系统中的进程状态(常用参数 ...
- excel导出简单示例(jxl jar包)
@param title excel文件名 @param excelTopName 表头中文名字(显示在第一行的中文表头内容) @param header 表头字段属性(根据该属性获取对应的属性值,表 ...
- Repeater的使用
1.页面代码 如果要分页,那么页面开头必须写(<%@ Register Src="~/Controls/Page.ascx" TagName="Page" ...
- postgres服务相关语法
远程登陆 psql -h 主机 -U 用户 -p端口 -W -d 数据库 #-W表示密码,例如:psql -h 192.168.137.3 -U postgres -p 5432 -W -d post ...
- 【leetcode】662. Maximum Width of Binary Tree
题目如下: Given a binary tree, write a function to get the maximum width of the given tree. The width of ...
- 复位和时钟控制(RCC)
一.系统复位 复位除了部分RCC寄存器和备份区域以外的其他所有的寄存器: 来源: NRST引脚上的低电平(外部复位) WWDG计数结束 IWDG计数结束 软件复位(通过NVIC) 低电压管理的复位 电 ...
- 2019牛客多校第五场H - subsequence 2 拓扑
H - subsequence 2 题意 要你使用前\(m\)个小写字母构造一个长度为\(n\)的字符串 有\(m*(m-1)/2\)个限制条件: \(c_{1} .c_{2}. len\):表示除去 ...