Spring boot 1.5.9 结合 Swagger2 2.7.0 一步一个脚印

Spring boot 1.5.9 结合 Swagger2 2.7.0 一步一个脚印
pom.xml

<dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.7.0version>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.7.0version>
        dependency>

SwaggerConfig.java

package com.gxk;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.gxk.swagger")) //Controller所在包(必须新建包)
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("")  //标题
                .description("")  //描述
                .termsOfServiceUrl("")  //超链接
                .contact("")   // 联系方式
                .version("1.0")
                .build();
    }
}

新建package com.gxk.swagger 并创建IndexController

package com.gxk.swagger;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value="gxk")
@ResponseBody 
public class IndexController {
    @RequestMapping(value="index", method=RequestMethod.GET)
    public String index(String pattern){
        return pattern;
    }
}   
http://localhost:8080/swagger-ui.html

这里写图片描述
Spring boot 1.5.9 结合 Swagger2 2.7.0 一步一个脚印_第1张图片
Spring boot 1.5.9 结合 Swagger2 2.7.0 一步一个脚印_第2张图片

接口和测试全都有了,很炫有木有!
Swagger更多内容,请访问swager官方网站

你可能感兴趣的:(swager,springboot)