apache / apache/dubbo

Steps for Programmatic integration of Swagger (API based configuration)

Open
#6,730 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
41.6k
Forks
26.4k
Avg merge
15h 13m
Merged PRs (30d)
4

Description

I'm building a Spring-less REST application using Apache Dubbo and RESTEasy and wanted to integrate swagger in my app. I followed the example given [at this link](https://github.com/apache/dubbo-samples/tree/master/java/dubbo-samples-rest), but this example uses a spring-based configuration file. Since my app is Spring-less and all the configurations (as shown below) is being done via API, I wanted to understand how to programmatically configure Swagger in Dubbo + RESTEasy app using API.

My Sample code is shown below.

### Environment

* Dubbo version: 2.7.7
* Operating System version: Ubuntu 18.04
* Java version: 1.8.x
* Programming Language: Scala
* Scala version: 2.11.x

### Steps to reproduce this issue

**1. Create a Sample Service to be listed in swagger**

```
import javax.ws.rs._
import javax.ws.rs.core.Context
import org.jboss.resteasy.spi.{HttpRequest, HttpResponse}

trait RestService {

@GET
protected def get(@Context request: HttpRequest,
@Context response: HttpResponse): Unit = {}
}

@Path("/test")
class MyService extends RestService {

override protected def get(request: HttpRequest,
response: HttpResponse): Unit = {
response.sendError(200, "All right V1!")
}
}
```

2. Configure the server using API configurations instead of xml file. This server will serve the above sample service and the predefined swagger service.

```
import org.apache.dubbo.config._
import org.apache.dubbo.config.bootstrap.DubboBootstrap
import org.apache.dubbo.rpc.protocol.rest.integration.swagger._

object Server {

def main(args: Array[String]): Unit = {

val appConfig = new ApplicationConfig("rest-provider")
appConfig.setQosEnable(false)

val regConfig = new RegistryConfig("n/a")
regConfig.setUseAsConfigCenter(true)
regConfig.setUseAsMetadataCenter(true)
regConfig.setGroup("myapps")
regConfig.setFile("./cache-dir/test")

val protoConfig1 = new ProtocolConfig("rest", 3300)
protoConfig1.setServer("netty")

val serviceConfig1 = new ServiceConfig[MyService]()
serviceConfig1.setRef(new MyService)
serviceConfig1.setInterface(classOf[RestService])
serviceConfig1.setProtocol(protoConfig1)
serviceConfig1.setVersion("1.0")
serviceConfig1.setId("service1")

val swagService = new ServiceConfig[DubboSwaggerApiListingResource]()
swagService.setRef(new DubboSwaggerApiListingResource)
swagService.setInterface(classOf[DubboSwaggerService])
swagService.setProtocol(protoConfig1)
swagService.setId("swservice1")

val dbs: DubboBootstrap = DubboBootstrap
.getInstance()
.application(appConfig)
.registry(regConfig)
.service(serviceConfig1)
.service(swagService)

dbs.start()
dbs.await()
}

}
```

3. Execute the above main method to get the server up and running
4. Open a web browser and go to [this link](http://localhost:3300/dubbo/swagger).

### Expected Result

What do you expect from the above steps?

The expected result should be a correct swagger.json listing. Showing the details of the above sample API.

### Actual Result

What actually happens?

Resultant JSON:

`{"swagger":"2.0","info":null,"host":null,"basePath":null,"tags":null,"schemes":null,"consumes":null,"produces":null,"security":null,"paths":null,"securityDefinitions":null,"definitions":null,"parameters":null,"responses":null,"externalDocs":null,"vendorExtensions":null,"securityRequirement":null}`

If there is an exception, please attach the exception trace:

```
2020-09-13 19:40:23.295+0530 - INFO - SwaggerContextService.java:90 || Exception caught testing servletConfig. see https://github.com/swagger-api/swagger-core/issues/1691

```

I have checked the above-mentioned issue, but I'm not sure how to correctly configure swagger using APIs. On further debugging, I saw that servlet config shows the below error line,

`Method threw 'org.jboss.resteasy.spi.LoggableFailure' exception. Cannot evaluate com.sun.proxy.$Proxy39.toString()`

Please help me with this. Thanks.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.