2

Have a web application running in tomcat in a war file and as part of it I have jax-rs rest apis implemented with jax-rs and bound by jersey. These rest apis have some swagger annotations, however I cannot seem to get the swagger-ui to function properly.

Strucure

warFile
+docs
----swagger is here
+web-inf
++web.xml
++lib
----all my jax-rs jars here
+meta-inf
----nothing

The jersey config I have defined

public class JerseyConfig extends ResourceConfig {

        public JerseyConfig() throws Exception {
            packages("com.myapp.somefiles");

            BeanConfig config = new BeanConfig();
            config.setBasePath("rest");
            config.setResourcePackage("com.myapp.somefiles");
            config.setScan(true);

        }

Web.xml

   <servlet>
        <servlet-name>com.myapp.somefile.JerseyConfig</servlet-name>
    </servlet>
    <servlet-mapping>
        <servlet-name>com.myapp.somefile.JerseyConfig</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

So when I have the swagger-ui html files in the war file I can access them with; localhost:8181/warFile/docs. However I would like to have this loaded on the same URL as my rest api which could be localhost:8181/warFile/rest/docs

I can access the swagger.json file at localhost:8181/warFile/rest/swagger.json

Do I need to load swagger with a classloader in the context of my servlet or beanconfig? The swagger-ui can also be packaged with one of the jar dependencies found in the /lib directory. Can I reference this somehow?

user2524908
  • 861
  • 4
  • 18
  • 46
  • Do you have index.html where you are providing swagger url? Something like http://localhost:8181/warFile/docs? – Suyash Oct 06 '16 at 03:19
  • Yes when I load localhost:8181/warFile/docs the swagger-ui will render correctly. What I am trying to do though is attach it to the same servlet or uri path as the jax-rs which is at 'localhost:8181/warFile/rest'. Is that possible, or even the right approach? – user2524908 Oct 06 '16 at 03:23
  • Is this server specifically dedicated to Swagger? If that's the case then you are going in right direction. What URL you have provided in index.html? – Suyash Oct 06 '16 at 03:36
  • The server is not specifically dedicated to swagger, I am packaging a war file with some jax-rs web services. Just want to know what is the best way to package the swagger-ui, should it just be a directory in the war file? – user2524908 Oct 06 '16 at 12:45
  • Let me rephrase my question. Will you be having only the rest api's on this server? Because if its not and still you want to keep same URL for accessing your other jsp/html files and swagger then there is no way to distinguish between them. – Suyash Oct 07 '16 at 03:33

1 Answers1

1

The easiest way to use Swagger UI is packing it into your WAR file: download the Swagger UI files from the GitHub repository and copy the content of the dist directory to the web content folder of your project. Then just update the index.html to point to your swagger.json.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359