eureka整合swagger2实现eureka界面点击服务跳转至swagger界面查看接口信息

client:
配置文件中添加status-page-url: http://localhost?{server.port}/swagger-ui.html#/
eureka整合swagger2实现eureka界面点击服务跳转至swagger界面查看接口信息
依赖:

      <!-- swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
        </dependency>

与启动类同目录下

package com.lzh.client;

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;
//注:该配置类需要在Application同级目录下创建,在项目启动的时候,就初始化该配置类
@Configuration
@EnableSwagger2 // 启用Swagger2
public class Swagger2 {
 
    @Bean
    public Docket createRestApi() {// 创建API基本信息
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.lzh.client"))// 扫描该包下的所有需要在Swagger中展示的API,@ApiIgnore注解标注的除外
                .paths(PathSelectors.any())
                .build();
    }
 
    private ApiInfo apiInfo() {// 创建API的基本信息,这些信息会在Swagger UI中进行显示
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")// API 标题
                .description("xxx提供的RESTful APIs")// API描述
                .contact("LZH")// 联系人
                .version("1.0")// 版本号
                .build();
    }
 
}

测试:

package com.lzh.client;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@EnableEurekaClient
@SpringBootApplication
@RestController
public class ClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }

    @Value("${server.port}")
    String port;

    @ApiOperation(value="获取xxx", notes="根据name获取xxx")// 使用该注解描述接口方法信息
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名字", required = true, dataType = "String", paramType="query")
    })
    // 使用该注解描述方法参数信息,此处需要注意的是paramType参数,需要配置成path,否则在UI中访问接口方法时,会报错
    // 注:paramType表示参数的类型,可选的值为"path","body","query","header","form"
    @RequestMapping(value = "/test1",method = RequestMethod.GET)
    public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }

    @ApiOperation(value="获取xxx", notes="根据test获取xxx")// 使用该注解描述接口方法信息
    @ApiImplicitParams({
            @ApiImplicitParam(name = "test", value = "测试", required = true, dataType = "String", paramType="path")
    })
    // 使用该注解描述方法参数信息,此处需要注意的是paramType参数,需要配置成path,否则在UI中访问接口方法时,会报错
    // 注:paramType表示参数的类型,可选的值为"path","body","query","header","form"
    @RequestMapping(value = "/test2/{test}",method = RequestMethod.GET)
    public String getString(@PathVariable String test) {
        return "asdas "+test;
    }

    //header 请求参数的获取:@RequestHeader
    //query 请求参数的获取:@RequestParam
    //path(用于restful接口) 请求参数的获取:@PathVariable

}   

启动服务端和客户端:
访问服务端
eureka整合swagger2实现eureka界面点击服务跳转至swagger界面查看接口信息
点击服务实例
eureka整合swagger2实现eureka界面点击服务跳转至swagger界面查看接口信息
进入swagger2界面即可查看当前服务的接口信息:
eureka整合swagger2实现eureka界面点击服务跳转至swagger界面查看接口信息
eureka整合swagger2实现eureka界面点击服务跳转至swagger界面查看接口信息