Redirecting to swagger-ui from JAX-RS


references

WebApp

swagger-ui를 다음과 같이 src/main/webapp 아래에 설치하였다.

.
|-- LICENSE
|-- nb-configuration.xml
|-- pom.xml
|-- README.md
`-- src
    |-- main
    |   |-- java
    |   |   `-- com
    |   |-- resources
    |   |   |-- com
    |   |   |-- META-INF
    |   |   `-- swagger
    |   |-- webapp
    |   |   |-- swagger-ui
    |   |   `-- WEB-INF
    |   `-- webapp-embedded-glassfish
    |       `-- WEB-INF
    `-- test
        `-- java

다음과 같은 주소들이 존재한다.

...://.../<context-root>/swagger-ui                 // swagger-ui path
...://.../<context-root>/<api-path>/swagger.json    // on-demand swagger.json
...://.../<context-root>/<api-path>/*               // jax-rs root resources

Post/Redirect/Get

@Path("/swagger-ui")
public class SwaggerUiResource {

    @GET
    public Response read() throws IOException {

        // ...://.../<context-root>/<application-path>/swagger.json
        final URI swaggerJson
            = uriInfo.getBaseUriBuilder()
            .path("swagger.json")
            .build();

        // ...://.../<context-root>
        final URI contextRoot
            = URI.create(servletRequest.getRequestURL().toString())
            .resolve(servletRequest.getContextPath());

        // ...://.../<context-root>/swagger-ui/index.html?url=swaggerJson
        final URI swaggerUi
            = UriBuilder.fromUri(contextRoot)
            .path("swagger-ui")
            .path("index.html")
            .queryParam("url", swaggerJson).build();

        // Post/Redirect/Get
        return Response.seeOther(swaggerUi).build();
    }

    @Context
    private UriInfo uriInfo;

    @Context
    private HttpServletRequest servletRequest;
}

Now when a user accesses to
.../<context-root>/<application-path>/swagger-ui
it redirects to
.../<context-root>/swagger-ui/index.html?url=.../<context-root>/<application-path>/swagger.json

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s