Servlet Startup Order in web.xml file

512 views
Skip to first unread message

Jennifer Coston

unread,
Apr 14, 2016, 5:01:40 PM4/14/16
to Swagger
Hello,

I noticed something strange in the swagger web.xml files and I was wondering if you can explain it to me. All of the additional servlets that the swagger specifications tell you to add have a <load-on-startup> value of 2. I did some research online and when that happens the servlets are started in the order they appear in the web.xml file. However, documentation doesn't specify if some servlets need to be started before others. Do I need to determine the best startup order? If so, what is the best way to go about doing that? 

Thank you!
-Jennifer

Ron Ratovsky

unread,
Apr 14, 2016, 5:19:05 PM4/14/16
to swagger-sw...@googlegroups.com
Which additional servlets exactly?

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jennifer Coston

unread,
Apr 14, 2016, 5:22:52 PM4/14/16
to Swagger
Here is my web.xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>


<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"


        xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">



        <servlet>


                <servlet-name>jersey</servlet-name>


                <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>


                <init-param>


                        <param-name>com.sun.jersey.config.property.packages</param-name>


                        <param-value>io.swagger.jaxrs.json;io.swagger.sample.resource;io.swagger.jaxrs.listing</param-value>


                </init-param>


                <init-param>


                        <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>


                        <param-value>com.sun.jersey.api.container.filter.PostReplaceFilter</param-value>


                </init-param>


                <init-param>


                        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>


                        <param-value>true</param-value>


                </init-param>


                <init-param>


                        <param-name>com.sun.jersey.config.feature.DisableWADL</param-name>


                        <param-value>true</param-value>


                </init-param>


                <load-on-startup>1</load-on-startup>


        </servlet>


        <servlet-mapping>


                <servlet-name>jersey</servlet-name>


                <url-pattern>/api/*</url-pattern>


        </servlet-mapping>



        <servlet>


                <servlet-name>DefaultJaxrsConfig</servlet-name>


                <servlet-class>io.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>


                <init-param>


                        <param-name>api.version</param-name>


                        <param-value>1.0.0</param-value>


                </init-param>


                <init-param>


                        <param-name>swagger.api.title</param-name>


                        <param-value>Hello World</param-value>


                </init-param>


                <init-param>


                        <param-name>swagger.api.basepath</param-name>


                        <param-value>http://localhost:8007/api</param-value>


                </init-param>


                <init-param>


                        <param-name>swagger.pretty.print</param-name>


                        <param-value>true</param-value>


                </init-param>


                <load-on-startup>2</load-on-startup>


        </servlet>



        <servlet>


                <servlet-name>Jersey2Config</servlet-name>


                <servlet-class>io.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>


                <init-param>


                        <param-name>api.version</param-name>


                        <param-value>1.0.0</param-value>


                </init-param>


                <init-param>


                        <param-name>swagger.api.basepath</param-name>


                        <param-value>http://localhost:8007/api</param-value>


                </init-param>


                <load-on-startup>2</load-on-startup>


        </servlet>


       


        <servlet>


                <servlet-name>Bootstrap</servlet-name>


                <servlet-class>io.swagger.sample.Bootstrap</servlet-class>


                <load-on-startup>2</load-on-startup>


        </servlet>


       


        <filter>


                <filter-name>ApiOriginFilter</filter-name>


                <filter-class>io.swagger.sample.util.ApiOriginFilter</filter-class>


        </filter>


        <filter-mapping>


                <filter-name>ApiOriginFilter</filter-name>


                <url-pattern>/*</url-pattern>


        </filter-mapping>


</web-app>




On Thursday, April 14, 2016 at 5:19:05 PM UTC-4, Ron wrote:
Which additional servlets exactly?




Ron Ratovsky

unread,
Apr 14, 2016, 5:35:15 PM4/14/16
to swagger-sw...@googlegroups.com
Looks like you’re using 3 different servlets to initialize Swagger, you need only one (I’d go with the Bootstrap out of the one you currently use).

As for the ordering ,once you get it down to 2, I’m not really sure if it matters, but that’s less combinations to test ;)

Date: Thursday, 14 April 2016 at 14:22
To: Swagger <swagger-sw...@googlegroups.com>
Subject: Re: Servlet Startup Order in web.xml file
Here is my web.xml file:

<?xmlversion="1.0"encoding="ISO-8859-1"?>


<web-appversion="2.4"xmlns="http://java.sun.com/xml/ns/j2ee"

Which additional servlets exactly?




Jennifer Coston

unread,
Apr 14, 2016, 5:43:14 PM4/14/16
to Swagger
I'm a bit confused then. I want to be able to generate the Swagger JSON and YAML files from the annotations in the code. From looking at the swagger-core documentation, it looks like you have to include the Jersey2Config servlet to be able to do this. I've started from one of the sample programs which had three servlets in it (jersey, DefaultJaxrsConfig, and bootstrap). Are you saying that I should get all of the functionality that I need with just the bootstrap and jersey servlets so my web.xml file would look like this?

<?xml version="1.0" encoding="ISO-8859-1"?>


<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"

                <servlet-name>Bootstrap</servlet-name>

                <servlet-class>io.swagger.sample.Bootstrap</servlet-class>

                <load-on-startup>2</load-on-startup>

        </servlet>

     


        <filter>

                <filter-name>ApiOriginFilter</filter-name>

                <filter-class>io.swagger.sample.util.ApiOriginFilter</filter-class>

        </filter>


        <filter-mapping>

                <filter-name>ApiOriginFilter</filter-name>

                <url-pattern>/*</url-pattern>

        </filter-mapping>

</web-app>


Thank  you for all of the help!
-Jennifer
...

Ron Ratovsky

unread,
Apr 14, 2016, 6:22:51 PM4/14/16
to swagger-sw...@googlegroups.com
Pretty much, yes. Which documentation are you referring to exactly? 

From: <swagger-sw...@googlegroups.com> on behalf of Jennifer Coston <jnco...@gmail.com>
Reply-To: "swagger-sw...@googlegroups.com" <swagger-sw...@googlegroups.com>
Date: Thursday, 14 April 2016 at 14:43
To: Swagger <swagger-sw...@googlegroups.com>
Subject: Re: Servlet Startup Order in web.xml file

I'm a bit confused then. I want to be able to generate the Swagger JSON and YAML files from the annotations in the code. From looking at the swagger-core documentation, it looks like you have to include the Jersey2Config servlet to be able to do this. I've started from one of the sample programs which had three servlets in it (jersey, DefaultJaxrsConfig, and bootstrap). Are you saying that I should get all of the functionality that I need with just the bootstrap and jersey servlets so my web.xml file would look like this?

--

Jennifer Coston

unread,
Apr 14, 2016, 6:27:40 PM4/14/16
to Swagger

Ron Ratovsky

unread,
Apr 14, 2016, 6:30:00 PM4/14/16
to swagger-sw...@googlegroups.com
"Only one of these methods should apply for your application.” :) So yeah, you really need one method to configure the system. If you notice, in those three methods you end up duplicating the same details – we definitely don’t require that.

Give a try and if something doesn’t work as expected, post back. 

From: <swagger-sw...@googlegroups.com> on behalf of Jennifer Coston <jnco...@gmail.com>
Reply-To: "swagger-sw...@googlegroups.com" <swagger-sw...@googlegroups.com>
Date: Thursday, 14 April 2016 at 15:27
To: Swagger <swagger-sw...@googlegroups.com>
Subject: Re: Servlet Startup Order in web.xml file

--

Jennifer Coston

unread,
Apr 15, 2016, 9:29:02 AM4/15/16
to Swagger
So, to get things working I had to have three servlets in my web.xml file: jersey, Jersey2Config, and Bootstrap. I can see the swagger.json file at the basepath (localhost:8007/api/). However, what I would really like is to have it also saved in a file somewhere so that I can reuse it to generate more code. Is there a way to specify that I want this to happen?

Since that description sounded a bit unclear the following graphic illustrates the process that I want to use:



Thank you!
Auto Generated Inline Image 1

Ron Ratovsky

unread,
Apr 15, 2016, 10:48:31 AM4/15/16
to swagger-sw...@googlegroups.com
If you’re looking for a java server that depends on a spec, instead of constantly going through the cycle (which you can), I’d suggest looking at https://github.com/swagger-api/swagger-inflector.

From: <swagger-sw...@googlegroups.com> on behalf of Jennifer Coston <jnco...@gmail.com>
Reply-To: "swagger-sw...@googlegroups.com" <swagger-sw...@googlegroups.com>
Date: Friday, 15 April 2016 at 06:29
To: Swagger <swagger-sw...@googlegroups.com>
Subject: Re: Servlet Startup Order in web.xml file

Auto Generated Inline Image 1

Jennifer Coston

unread,
Apr 15, 2016, 10:56:33 AM4/15/16
to Swagger
I've looked at swagger-inflector and it seems to do a lot of the processing behind the scenes. For now, I think I want to control the generation process so that I can customize it as needed. So, what do I need to do to have swagger-core also create a swagger.json file that I can easily access?

Ron Ratovsky

unread,
Apr 15, 2016, 11:02:17 AM4/15/16
to swagger-sw...@googlegroups.com
Swagger-core only produces the file at runtime. You can look at tools like https://github.com/kongchen/swagger-maven-plugin to generate it in compile-time.

From: <swagger-sw...@googlegroups.com> on behalf of Jennifer Coston <jnco...@gmail.com>
Reply-To: "swagger-sw...@googlegroups.com" <swagger-sw...@googlegroups.com>
Date: Friday, 15 April 2016 at 07:56
To: Swagger <swagger-sw...@googlegroups.com>
Subject: Re: Servlet Startup Order in web.xml file

Jennifer Coston

unread,
Apr 15, 2016, 11:08:33 AM4/15/16
to swagger-sw...@googlegroups.com

Thank you! I've been really impressed with the quality of support you guys provide. I will look into those and post back if I run into any issues.

Best,

- Jennifer

Reply all
Reply to author
Forward
0 new messages