Ask a Question

Adding Swagger to an existing java application

gcorsar
Occasional Contributor

Adding Swagger to an existing java application

I've read through a bunch of the swagger documentation but can't seem to find a good discussion about adding swagger to an existing java application. I need to have it work stand-alone because the application is behind a firewall. Can someone point me in the right direction? I've downloaded the swagger-ui-dist package since from what I've read that would be the package I need for a stand-alone application.

4 REPLIES 4
abonstrom
Staff

Re: Adding Swagger to an existing java application

Hi @gcorsar,

 

It sounds like you want to generate a Swagger/OpenAPI document based off of your currently existing Java project, is this correct? If so there are a couple different options to achieve this. 

 

If you are using a particular framework, there may already exist a tool to automatically generate documentation based off your code with no extra code needed. For java, there exists swagger-core if you're using JAX-RS, and springfox if you're using Spring. 

 

Otherwise, it is possible to use a Swagger annotations tool to generate Swagger/OpenAPI documents. For this option, you would need to annotate your code properly with all the necessary information you want to add to your documentation. But after a little overhead work, you would be able to generate documentation automatically in your build process. You can read up more on this here: https://swagger.io/blog/api-development/swagger-annotation-libraries/ 

gcorsar
Occasional Contributor

Re: Adding Swagger to an existing java application

First, thank you for the response.

I've been able to add swagger, but up to a point. I've annotated the Java code and have added the dist folder to the project. I am able to get the json/yaml output and have also been able to display the Swagger UI, but the URL to the actual endpoint is wrong. I've tried various options but can't seem to get the Swagger UI to point to the correct URL for GET requests. I think it's a configuration issue but can't figure it out.

abonstrom
Staff

Re: Adding Swagger to an existing java application

@gcorsar,

 

Is this what you mean? The URL you want to query against is not available in the drop down in Swagger UI?

abonstrom_0-1637604905493.png

 If so, you need to add the proper URL to the available servers on your document. I'm not sure how to achieve this using a Swagger annotation tool, but it should be possible by editing the Swagger/OpenAPI document directly. 

 

What you need to do is edit your "servers" tag to look like so:

abonstrom_1-1637605003550.png

Every value you add as a url to your servers tag will be added as an option to the dropdown list when the document is rendered in SwaggerUI. That way, when you "Try It Out" on one of your operations, the API call will be sent to the address you specified.

gcorsar
Occasional Contributor

Re: Adding Swagger to an existing java application

That might be a solution but it doesn't provide runtime flexibility. I noticed that in the index.html I can use variables to get the protocol/host/port but I don't see that possibility in the configuration file. Maybe I'm missing something though.

cancel
Showing results for 
Search instead for 
Did you mean: