All Products
Document Center

Integration of SOFARPC RESTful and Swagger

Last Updated: Aug 17, 2020

From rpc-sofa-boot-starter 6.0.1, you can use SOFARPC to integrate RESTful services and Swagger.

To activate Swagger when rpc-sofa-boot-starter is enabled, first add Swagger dependencies in pom.xml:

  1. <dependency>
  2. <groupId>io.swagger.core.v3</groupId>
  3. <artifactId>swagger-jaxrs2</artifactId>
  4. <version>2.0.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId></groupId>
  8. <artifactId>guava</artifactId>
  9. <version>20.0</version>
  10. </dependency>

Then, add in

Last, access http://localhost:8341/swagger/openapi to obtain the Swagger OpenAPI of SOFARPC RESTful.

If rpc-sofa-boot-starter is not enabled or the version of rpc-sofa-boot-starter is earlier than 6.0.1, you can integrate Swagger in the following way.

First, introduce Swagger dependencies in the app. You only need to introduce the JAXRS dependency of Swagger because the RESTful protocol of SOFARPC uses the JAXRS standard.

  1. <dependency>
  2. <groupId>io.swagger.core.v3</groupId>
  3. <artifactId>swagger-jaxrs2</artifactId>
  4. <version>2.0.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId></groupId>
  8. <artifactId>guava</artifactId>
  9. <version>20.0</version>
  10. </dependency>

Guava 20.0 is introduced to avoid version conflicts.

Add a Swagger RESTful service

To enable Swagger to expose the RESTful service of SOFARPC through Swagger OpenAPI, you can provide the OpenAPI service of Swagger through the RESTful service of SOFARPC. First, create an interface:

  1. @Path("swagger")
  2. public interface OpenApiService {
  3. @GET
  4. @Path("openapi")
  5. @Produces("application/json")
  6. String openApi();
  7. }

Then, provide an implementation class, and publish it as a RESTful service of SOFARPC:

  1. @Service
  2. @SofaService(bindings = {@SofaServiceBinding(bindingType = "rest")}, interfaceType = OpenApiService.class)
  3. public class OpenApiServiceImpl implements OpenApiService, InitializingBean {
  4. private OpenAPI openAPI;
  5. @Override
  6. public String openApi() {
  7. return Json.pretty(openAPI);
  8. }
  9. @Override
  10. public void afterPropertiesSet() {
  11. List<Package> resources = new ArrayList<>();
  12. resources.add(this.getClass().getPackage()); // Scan the package where the current class is located, or the package where other RESTful service interfaces of SOFARPC are located.
  13. if (!resources.isEmpty()) {
  14. // init context
  15. try {
  16. SwaggerConfiguration oasConfig = new SwaggerConfiguration()
  17. .resourcePackages(;
  18. OpenApiContext oac = new JaxrsOpenApiContextBuilder()
  19. .openApiConfiguration(oasConfig)
  20. .buildContext(true);
  21. openAPI =;
  22. } catch (OpenApiConfigurationException e) {
  23. throw new RuntimeException(e.getMessage(), e);
  24. }
  25. }
  26. }
  27. }

After the app is started, you can access http://localhost:8341/swagger/openapi to obtain the information about all RESTful services published by the app.

Solve the cross-origin access problem of SOFARPC RESTful service

If you start a Swagger UI on another port and want to access http://localhost:8341/swagger/openapi by using the Swagger UI to view the API definition and initiate a call, you need to solve the cross-origin access problem of the SOFARPC RESTful service by adding the following code before starting the app:

  1. import org.jboss.resteasy.plugins.interceptors.CorsFilter;
  2. public static void main(String[] args) {
  3. CorsFilter corsFilter = new CorsFilter();
  4. corsFilter.getAllowedOrigins().add("*");
  5. JAXRSProviderManager.registerCustomProviderInstance(corsFilter);
  6., args);
  7. }

In this way, you can use the Swagger UI to access the Swagger OpenAPI provided by the app across domains.