springMVC4.3.7整合 swagger2 2.6.1
来源:互联网 发布:卫星电视网络在线直播 编辑:程序博客网 时间:2024/05/16 03:20
springMVC4.3.7整合 swagger2 2.6.1
因为老大说写接口文档什么的太low了,所以说让我看下这个相对来讲比较酷炫的swagger-ui,其实我也没有用过很懵逼,所以看了半天还是一直在坑里.
- 首先是集成swagger需要两个最基本的jar包,这里用的2.6的应该是很新的了
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency>
你如果项目中没有json包那么你可能需要这些
<!-- jackson --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency>
但是注意这里有问题 ,我就在这徘徊了好久:springfox-swagger-ui这个包是固定的
但是springfox-swagger2也可以用
<dependency> <groupId>com.mangofactory</groupId> <artifactId>swagger-springmvc</artifactId> <version>1.0.2</version> </dependency>
这个包来替换。但是不是太建议用↑这个包,配置起来相对于springfox-swagger2这个些许麻烦了一点.你需在Github上下载文件放到项目中(https://github.com/swagger-api/swagger-ui)dist文件夹下的文件放到你的项目中,其实接口管理页面已经备集成到springfox-swagger-ui-2.6.1.jar这个jar包中
就在
因为这个东西默认是英文的所以你要汉化一下就把↓这两段放进这个swagger-ui.html的head里面.
顺便说说一下怎么改jar包内容,首先你需要找到你的maven本地库这个springfox-swagger-ui-2.6.1.jar这个jar的位置然后直接f2改成rar格式,然后解压找到文件修改然后MEAT-INF开始打包格式zip,包名和之前一致然后f2改成jar就好了
<script src='webjars/springfox-swagger-ui/lang/translator.js' type='text/javascript'></script> <script src='webjars/springfox-swagger-ui/lang/zh-cn.js' type='text/javascript'></script>
2.因为sprigmvc对于静态资源的访问有限制所以你需要在spring-xml里面放行
<!-- 配置Swagger相关静态资源 --> <mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/> <mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/> <!-- 引用Swagger 默认配置 --> <bean class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration" id="swagger2Config"/> <!-- 添加Swagger2 扫描目录 --> <!--这里只是扫描注解如果你在此前配置了spring关于注解扫描可以忽略--> <!-- <context:component-scan base-package="com.scf.*.*" /> -->
!!!当你重启项目发现报错找不到这个bean,那你就用clean然后重新编译一下
3.接下来就是注释部分(这些注释可以ctrl+左键点进去看看具体都有哪些参数具体作用里面都会有注释)注解里用那些字段看自己需求。
/** *@Api value 对当前control层的一个说明 * @ApiOperation value 对当前接口的一个注释 notes 对当前接口的备注 httpMethod 请求类型 produces 返回数据的响应类型格式 * @ApiParam value 对接受参数的备注 required 在swagger页面测试时是否为必须要传参数 name=参数名称 * @ApiImplicitParams作用可以放多个 @ApiImplicitParam对多个参数进行说明 * @ApiParam 是对接口的参数进行说明 * */
对你实体类做说明的时候就这俩
@ApiModel(value="实体类的说明",description="补充说明")@ApiModelProperty(value="字段解释",name="属性名称",example="例子")
4.当你配置完成的时候
http://localhost:8080/项目名称/swagger-ui.html
就差不多了!
5.这些字段说明都是可以通过api去设置]!可以粗略的对比下区别
6.httpMethod
7.paramType
!!!!!我是参考的这位大佬的
http://blog.csdn.net/xupeng874395012/article/details/68946676!!!!
8.这里就是返回的json
over!~
- springMVC4.3.7整合 swagger2 2.6.1
- SpringMvc4集成swagger2
- springMVC4集成swagger2
- Spring4.2.6+SpringMVC4.2.6+MyBatis3.4.0 整合
- springmvc+swagger2整合
- Spring MVC 整合 Swagger2
- Spring Boot整合Swagger2
- springmvc 整合 swagger2
- spring boot整合Swagger2
- SpringBoot整合Swagger2
- SpringMVC整合Swagger2
- spring boot 整合Swagger2
- SpringMVC整合swagger2
- springmvc4 整合 tiles3
- SpringMVC4+Spring+Hibernate4整合
- SpringMvc4+Mybatis整合
- Mybatis3+Spring4+SpringMVC4 整合
- Mybatis3+Spring4+SpringMVC4 整合
- service启动方式
- Scala 中应用 Future 并发编程
- 分享一个问题的解决方法IllegalArgumentException: Viewnot attached to window manager
- android_fragment加载
- 使用servlet+jsp实现简单的网络通信
- springMVC4.3.7整合 swagger2 2.6.1
- NYOJ 括号配对问题(不用栈)
- Ubuntu 16.04下Intel SGX应用程序程序开发——获得OCALL调用的返回值
- overflow滚动条无效
- 带你一起探究Retrofit 源码,让你不再畏惧Retrofit的面试提问
- 设计模式——享元模式
- oracle修改多张表的字段及值
- 如何在提交代码时去掉忽略性文件
- 风投在中国