亲宝软件园·资讯

展开

spring集成swagger SpringMVC集成Swagger实例代码

catoop 人气:0
想了解SpringMVC集成Swagger实例代码的相关内容吗,catoop在本文为您仔细讲解spring集成swagger的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:spring集成swagger,spring4,集成swagger,spring,swagger2,下面大家一起来学习吧。

此前写过一个关于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

加载全部内容

相关教程
猜你喜欢
用户评论