Giter VIP home page Giter VIP logo

grpc-swagger-springdoc-openapi's Introduction

Grpc Swagger Springdoc Openapi

Debugging gRPC clients with swagger-ui on Spring Boot 3.

Swagger for gRPC Clients.

Note: This project developed with springdoc-openapi v2.0.0 because This project Just supported on Spring Boot v3.0.0 ( and above). I am also using this library net.devh:grpc-spring-boot-starter:2.14.0.RELEASE (thank you for that).

I am using on my open-source microservice project. You can see how to work.

https://github.com/tdilber/anouncy/blob/master/backend/location/src/main/java/com/beyt/anouncy/location/config/GrpcSwaggerConfig.java Screen Shot 2022-12-17 at 23 41 10 Screen Shot 2022-12-17 at 23 39 51

Usage:

Just do this 3 step:

1

<dependencies>
    ...
    <dependency>
        <groupId>com.beyt.doc</groupId>
        <artifactId>grpc-swagger-springdoc-openapi</artifactId>
        <version>0.0.1.1</version>
    </dependency>
</dependencies>

<repositories>
    <repository>
        <id>github2</id>
        <name>GitHub Apache Maven Packages</name>
        <url>https://maven.pkg.github.com/tdilber/grpc-swagger-springdoc-openapi</url>
    </repository>
</repositories>

Note: This dependency not working for public. I will serve with maven central repo as soon as possible. (Sorry for that.)

2

Just Add the annotation on Spring Boot Application annotation. (Example of 2 Service configuration).

There are 2 type of usage:

  • createGrpcController => When exception occurred then throw exception.
  • createGrpcControllerSafely => When exception occurred then create default class for not throw exception.
@Configuration
public class GrpcSwaggerConfig {

    @Bean
    public GrpcRepositoryCreator grpcRepositoryCreator() {
        return new GrpcRepositoryCreator(); // use same creator because when same parameter classes usage will not create more than one. 
    }

    @Bean
    public Object createControllerVote(GrpcRepositoryCreator grpcRepositoryCreator) {
        return grpcRepositoryCreator.createGrpcController(VotePersistServiceGrpc.VotePersistServiceBlockingStub.class, //Grpc Stub Class 
                "persist-grpc-server", //grpc server config name
                "vote"); // prefix for same method name usage
    }

    @Bean
    public Object createControllerAnnounce(GrpcRepositoryCreator grpcRepositoryCreator) {
        return grpcRepositoryCreator.createGrpcControllerSafely(AnnouncePersistServiceGrpc.AnnouncePersistServiceBlockingStub.class, //Grpc Stub Class
                "persist-grpc-server", //grpc server config name
                "announce");  // prefix for same method name usage
    }
}

Sample Application Yaml File:

grpc:
  client:
    persist-grpc-server: # the grpc server config name
      address: 'static://127.0.0.1:9096'
      enableKeepAlive: true
      keepAliveWithoutCalls: true
      negotiationType: plaintext

3

Standart swagger dependency for Spring Boot 3.

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.0.0</version>
</dependency>

How To Work

I used Byte-Buddy tool for creating RestController with POST endpoints for every gRPC service methods.

  • First I am converting to all method returns and method parameters objects (proto object what have builders) to POJOs.
  • Second I am converting all services method in the single Rest Controller class with the POJOs. (With method interceptor.)
  • Third intercetor does this 4 step when request came =>
1- convert param POJO => Proto Object
2- invoke Proto Client With Proto Object (Returning Proto Object)
3- convert Returned Proto Object to Return POJO object
4- return The POJO object to the endpoint result

Note: This conversion is using json serialization.

grpc-swagger-springdoc-openapi's People

Contributors

tdilber avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

grpc-swagger-springdoc-openapi's Issues

The repo not works

Following your comments your, repository to download your grpc-swagger-springdoc-openapi library not works

Problems with grpc-server-spring-boot-starter

Using the grpc-server-spring-boot-starter version 2.14.0.RELEASE to start the gRPC Server not start with Springboot 3, so you must to use Springboot 2, but then springdoc-openapi v2.0.0 not works, so how resolve this inconsistency?

Dependency to net.devh

Why you are using this dependency which use spring 2.xxx but you are using spring boot 3 ? I think there is no reason to use this and you can create your own annotation to find grpc clients

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.