Home  >  Article  >  Java  >  Detailed explanation of the steps to integrate Swagger with java

Detailed explanation of the steps to integrate Swagger with java

Y2J
Y2JOriginal
2017-05-09 11:27:113597browse

This article mainly introduces the SpringMVC integrated Swagger example code. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let’s follow the editor and take a look.

I have previously written a post about SpringBoot integrating Swagger. Because some projects are SpringMVC, I briefly sorted them out and they are basically the same.

This example uses spring 4.1.6 version

1. Add POM dependency

  <!-- 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. Add SwaggerConfig.java class

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. Add configuration file

<?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. Test Controller method

@Controller
public class HomeController {

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

  return "test";
 }

}

5. Start service access to view the effect

Access address: localhost:8188/{project contextPath}/swagger-ui .html

【Related recommendations】

1. Java Free Video Tutorial

2. Comprehensive analysis of Java annotations

3. YMP Online Manual

The above is the detailed content of Detailed explanation of the steps to integrate Swagger with java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn