springmvc+swagger2

简介:

v一、swagger2依赖

复制代码
<!--swagger-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <exclusions>
        <exclusion>
            <artifactId>spring-aop</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
            <artifactId>jackson-annotations</artifactId>
            <groupId>com.fasterxml.jackson.core</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
</dependency>
复制代码

v二、springmvc配置文件加入

<mvc:default-servlet-handler />

v三、web.xml配置

<welcome-file-list>
    <welcome-file>swagger-ui.html</welcome-file>
</welcome-file-list>

v四、swagger2配置

  可创建多个Docket,对restful api进行分组管理

复制代码
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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 springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableWebMvc
@EnableSwagger2
@Configuration
public class Swagger2Config extends WebMvcConfigurationSupport {

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("admin")
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.ant("/**"))
                .build();
    }

//    @Bean
//    public Docket xxx() {
//        return new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(apiInfo())
//                .groupName("xxx")
//                .select()
//                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
//                .paths(PathSelectors.ant("/xxx/**"))
//                .build();
//    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("shiro 无状态组件")
                .contact(new Contact("胡俊哲个", "", "2570230521@qq.com"))
                .version("1.0")
                .build();
    }
}
复制代码

v五、效果演示

Restful API 访问路径:  
 * http://IP:port/{context-path}/swagger-ui.html

v六、注意事项

  1、如果有拦截器或者过滤器 对项目根路径进行拦截,可能<welcome-file>的配置不生效!

  2、如果 配置 <welcome-file>swagger-ui.html</welcome-file>,访问 http://IP:port/{context-path}/,虽然可以看到页面,但是接口内容显示不出来。原因如下:

  

  这个js在匹配url的时候 要包含“swagger-ui.html”,所以就出错了。解决方案如下:

  (1)修改web.xml

 <welcome-file-list>
     <welcome-file>index.html</welcome-file>
 </welcome-file-list>

  (2)新建index.html

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<script type="application/javascript">
    window.location.href = window.location.origin + "/shiro/swagger-ui.html";
</script>
</body>
</html>
复制代码

v七、swagger2常用注解参考

  Swagger-Core Annotations










本文转自 小眼儿 博客园博客,原文链接:http://www.cnblogs.com/hujunzheng/p/7210140.html,如需转载请自行联系原作者
目录
相关文章
|
JSON 前端开发 fastjson
SpringMVC中使用FastJsonHttpMessageConverter时Swagger2失效的解决办法
SpringMVC中使用FastJsonHttpMessageConverter时Swagger2失效的解决办法
427 0
|
Java API Spring
MP实战系列(十)之SpringMVC集成SpringFox+Swagger2
该示例基于之前的实战系列,如果公司框架是使用JDK7以上及其Spring+MyBatis+SpringMVC/Spring+MyBatis Plus+SpringMVC可直接参考该实例。 不过建议最好采用的是JDK8+Spring+MyBatis Plus+SpringMVC,因为本示例就是基于这个。
1697 0
|
API
springmvc+swagger构建Restful风格文档
  本次和大家分享的是java方面的springmvc来构建的webapi接口+swagger文档;上篇文章分享.net的webapi用swagger来构建文档,因为有朋友问了为啥.net有docpage文档你还用swagger,这里主要目的是让接口文档统一,当操作多种开发语言做接口时,如果有统一风格的api文档是不是很不错;还有就springcloude而言,微服务如果有很多的话,使用swagger自动根据服务serverid来加载api文档是很方便的。
1462 0
|
JSON API 数据格式
SpringMVC 中配置 Swagger 插件.
一、简介  Swagger的目标是为REST API定义一个与语言无关的标准接口,允许用户发现和理解计算机服务的功能,而无需访问源代码。当通过Swagger正确定义时,用户可以用最少量的实现逻辑理解远程服务并与之交互。
1571 0
|
XML 前端开发 Java
SpringMVC+Swagger详细整合
一、新建maven工程导入正确的pom文件 还是那句话,包导入正确就成功了80%。剩下的20%慢慢攻克吧。 4.0.0 com.apidoc.demotest apidoc 0.0.
1351 0
SpringMVC集成Swagger
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/catoop/article/details/58630864 此前写过一个关于SpringBoot集成Swagger的帖子,因为有的项目是SpringMVC的,所以也简单整理了一下,基本一致。
1149 0
|
前端开发 Java API
dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb3.4-redis3(十)之Spring MVC中使用 Swagger2 构建Restful API
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010046908/article/details/55047193 1、Swagger2是什么? Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件。
1241 0
|
JSON fastjson 数据格式
SpringMVC中使用FastJsonHttpMessageConverter时Swagger2失效的解决办法
2016-08-01补充:该问题解决已合并入fastjson 1.2.15版本,请使用1.2.15+版本就不需要做下面的改造了 FastJson是阿里巴巴开源的高性能JSON转换工具。
1394 0
下一篇
DDNS