5.12OpenAPI / Swagger Support

 

In HAPI FHIR, support for OpenAPI (aka Swagger) is supported via the OpenApiInterceptor.

Note that this interceptor supports servers using the RestfulServer (aka HAPI FHIR Plain Server and JPA Server), and does not currently support JAX-RS servers.

When this interceptor is registered against the server, it performs the following 3 tasks:

System Functionality

  • OpenAPI 3.0 Documentation will be served at [baseUrl]/api-docs. This documentation is generated by the interceptor using information from the server's CapabilityStatement as well as from its automatically generated OperationDefinitions.

User Functionality

  • Anytime a user using a browser navigates to the Base URL of the server, they will be automatically redirected to [baseUrl]/swagger-ui/

  • A customized version of the Swagger UI tool will be served at [baseUrl]/swagger-ui/

5.12.1Enabling OpenAPI

 

The HAPI FHIR OpenAPI functionality is supplied in a dedicated module called hapi-fhir-server-openapi. To enable this functionality you must first include this module in your project. For example, Maven users should include the following dependency:

<dependency>
	<groupId>ca.uhn.hapi.fhir</groupId>
	<artifactId>hapi-fhir-server-openapi</artifactId>
	<version>VERSION</version>
</dependency>

You then simply have to register the interceptor against your RestfulServer instance.

@WebServlet(urlPatterns = { "/fhir/*" }, displayName = "FHIR Server")
public class RestfulServerWithOpenApi extends RestfulServer {

   @Override
   protected void initialize() throws ServletException {

      // ... define your resource providers here ...

      // Now register the interceptor
      OpenApiInterceptor openApiInterceptor = new OpenApiInterceptor();
      registerInterceptor(openApiInterceptor);

   }

}

5.12.2Demonstration

 

See the HAPI FHIR Test Server for a demonstration of HAPI FHIR OpenAPI functionality: http://hapi.fhir.org/baseR4/swagger-ui/