Forum Discussion

gcorsar's avatar
gcorsar
Occasional Contributor
3 years ago

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

  • 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's avatar
      gcorsar
      Occasional Contributor

      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's avatar
        abonstrom
        Staff

        gcorsar,

         

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

         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:

        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.