SpringBoot,SpringCloud集成Swagger文档生成器

SpringBoot2.2.4.RELEASE集成Swagger
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.0.8.RELEASE</version>
</dependency>

<dependency>
    <groupId>com.spring4all</groupId>
    <artifactId>swagger-spring-boot-starter</artifactId>
    <version>1.9.1.RELEASE</version>
</dependency>


#添加扫包
swagger.base-package=com.example.springbootswagger.controller


/**
 * http://127.0.0.1:8080/swagger-ui.html
 */
@SpringBootApplication
@EnableSwagger2Doc
public class SpringbootSwaggerApplication {

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

}

@RestController
@Api("测试接口服务")
public class TestController {

    @GetMapping("/one")
    @ApiOperation("测试get-单独传参")
    @ApiImplicitParam(name="name",value = "用户名")
    public Object testOne(String name){
        return name;
    }


    @PostMapping("/two")
    @ApiOperation("测试post-多参数")
    public Object testTwo(@RequestBody User user){
        return user;
    }
}


public class User {
/**
 * 多参数可用对象封装 用@ApiModelProperty注解生成标准json文档
 */
    @ApiModelProperty(name = "name",value = "用户名")
    private String name;
    @ApiModelProperty(name = "address",value = "家庭住址")
    private String address;
}

  SpringCloud集成Swagger:

注意springcloud版本:各个版本之间差异巨大
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
    <relativePath/>
</parent>
--------------------------------------------------------------------------------

<dependency>
    <groupId>com.spring4all</groupId>
    <artifactId>swagger-spring-boot-starter</artifactId>
    <version>1.9.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>26.0-jre</version>
</dependency>
--------------------------------------------------------------------------------
各个微服务集成方式同SpringBoot一致

区别主要是Zuul的集成:
--------------------------------------------------------------------------------
package com.example.zuulservice.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * zuul 集成 多个服务 swagger
 */
@Component
@Primary
class DocumentationConfig implements SwaggerResourcesProvider {

    // zuul配置能够使用config实现实时更新
    @RefreshScope
    @ConfigurationProperties("zuul")
    public ZuulProperties zuulProperties() {
        return new ZuulProperties();
    }

    @Override
    public List<SwaggerResource> get() {
        List resources = new ArrayList<>();
        // aapi-member-service name可以自定义 location serviceName 根据自己情况写 后边拼接写死
        resources.add(swaggerResource("api-member-service", "/api-member/v2/api-docs", "2.0"));
        resources.add(swaggerResource("api-order-service", "/api-order/v2/api-docs", "2.0"));
        return resources;
    }

    private SwaggerResource swaggerResource(String name, String location, String version) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);
        swaggerResource.setLocation(location);
        swaggerResource.setSwaggerVersion(version);
        return swaggerResource;
    }
}
Zull也开启Swagger:
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableSwagger2Doc
public class ZuulServiceApplication {
    /**
     * 搭建zuul网关服务
     * 根据网关调用接口 http://localhost:8765/swagger-ui.html
     * http://127.0.0.1:8765/api-order/order/getUsersByFegin
     * @param args
     */

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

}

即可完成Zuul与个服务之间的集成

访问地址:http://localhost:8765/swagger-ui.html

注意fegin的传参:
1.interface:
@PostMapping("/user/insertUser")
public JSONObject insertUser(@RequestBody Map<String, String> map);



2.controller:
@PostMapping("/insertUser")
@ApiOperation("测试分布式事务框架lcn")
public int insertUser(@RequestBody User user){
System.out.println(user.getName());
String name =user.getName();
userService.insert(user);
return 1;
}

  

 

posted @ 2020-02-26 12:01  洞玄巅峰  阅读(807)  评论(0编辑  收藏  举报