SpringMVC集成Swagger实例代码

所属分类: 软件编程 / java 阅读数: 20
收藏 0 赞 0 分享

此前写过一个关于SpringBoot集成Swagger的帖子,因为有的项目是SpringMVC的,所以也简单整理了一下,基本一致。

本例使用的是spring 4.1.6版本

1、添加POM依赖

  <!-- Jackson -->
  <dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-core</artifactId>
   <version>2.5.3</version>
  </dependency>
  <dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-annotations</artifactId>
   <version>2.5.3</version>
  </dependency>
  <dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.5.3</version>
  </dependency>

  <!-- Swagger -->
  <dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.6.1</version>
  </dependency>

  <dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.6.1</version>
  </dependency>

2、添加SwaggerConfig.java类

package com.shanhy.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.base.Predicate;

import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration // 该注解就是告诉Spring这个是一个配置文件类,这里配置的Bean要交给Spring去管理。这个就是用来取代Beans.xml这种文件的。
@EnableSwagger2 // 启用 Swagger
public class SwaggerConfig {

 @Bean
 public Docket createRestApi() {
  Predicate<RequestHandler> predicate = new Predicate<RequestHandler>() {
   @Override
   public boolean apply(RequestHandler input) {
    Class<?> declaringClass = input.declaringClass();
    // if (declaringClass == BasicErrorController.class)// 排除
    // return false;
    if (declaringClass.isAnnotationPresent(RestController.class)) // 被注解的类
     return true;
    if (input.isAnnotatedWith(ResponseBody.class)) // 被注解的方法
     return true;
    return false;
   }
  };
  return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
    // .genericModelSubstitutes(DeferredResult.class)
    // .genericModelSubstitutes(ResponseEntity.class)
    .useDefaultResponseMessages(false)
    // .forCodeGeneration(false)
    .select().apis(predicate)
    // .paths(PathSelectors.any())//过滤的接口
    .build();
 }

 private ApiInfo apiInfo() {
  return new ApiInfoBuilder().title("接口服务")// 大标题
    .version("1.0")// 版本
    .build();
 }
}

3、配置文件添加

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:beans="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/mvc 
   http://www.springframework.org/schema/mvc/spring-mvc.xsd  
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


 <!-- 这里省略了其他原来的配置内容 -->
 ......
 ......
 ......
 ......

 <mvc:default-servlet-handler />

</beans:beans>

4、测试Controller方法

@Controller
public class HomeController {

 @RequestMapping(value = "/test", method = RequestMethod.GET)
 @ResponseBody
 public String test(Locale locale, Model model) {

  return "test";
 }

}

5、启动服务访问查看效果

访问地址:http://localhost:8188/{工程contextPath}/swagger-ui.html

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Collections工具类_动力节点Java学院整理

Collections工具类提供了大量针对Collection/Map的操作。这篇文章主要介绍了Collections工具类_动力节点Java学院整理,需要的朋友可以参考下
收藏 0 赞 0 分享

SpringMVC集成Swagger实例代码

本篇文章主要介绍了SpringMVC集成Swagger实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

十大常见Java String问题_动力节点Java学院整理

本文介绍Java中关于String最常见的10个问题,需要的朋友参考下吧
收藏 0 赞 0 分享

Java微信公众平台开发(13) 微信JSSDK中Config配置

这篇文章主要为大家详细介绍了Java微信公众平台开发第十三步,微信JSSDK中Config配置,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java实现一个达达租车系统的步骤详解

这篇文章主要给大家介绍了利用Java实现一个达达租车系统的步骤,文中给出了详细的实现思路和示例代码,并在文末给出了完整的源码供大家学习下载,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

Java微信公众平台开发(14) 微信web开发者工具使用

这篇文章主要为大家详细介绍了Java微信公众平台开发第十四步,微信web开发者工具的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Spring Boot整合RabbitMQ实例(Topic模式)

Topic Exchange 转发消息主要是根据通配符。接下来通过本文给大家分享Spring Boot整合RabbitMQ实例(Topic模式),需要的朋友参考下吧
收藏 0 赞 0 分享

Java微信公众平台开发(15) 微信JSSDK的使用

这篇文章主要为大家详细介绍了Java微信公众平台开发第十五步,微信JSSDK的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java多线程的同步方法实例代码

这篇文章主要介绍了 java多线程的同步方法实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

spring boot整合RabbitMQ实例详解(Fanout模式)

这篇文章主要介绍了spring boot整合RabbitMQ的实例讲解(Fanout模式),非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多