Confusion between HttpServlet class and using it with Jersey

后端 未结 2 1147
小蘑菇
小蘑菇 2021-01-03 02:41

I am building servlets which implement a RESTful API. I understand the Jersey is a framework for deciphering and using given URL\'s. How do I use it in conjunction with the

相关标签:
2条回答
  • 2021-01-03 03:08

    Jersey uses a servlet to route URLs to the appropriate service. Your service itself does not need to extend a servlet.

    At a high level, Jersey's ServletContainer class accepts the requests, and then based on your Jersey configuration, your web service will be invoked. You configure what url patterns are processed by Jersey. Check out section 5.3 http://www.vogella.com/articles/REST/.

    0 讨论(0)
  • 2021-01-03 03:18

    Actually you are confused because you don't understand how jersey works. Jersey framework basically uses com.sun.jersey.spi.container.servlet.ServletContainer servlet to intercept all the incoming requests. As we configure in our projects web.xml, that all the incoming rest request should be handled by that servlet. There is an init-param that is configured with the jersey servlet to find your REST service classes. REST service classes are not Servlet and they need NOT to extend the HttpServlet as you did in your code. These REST service classes are simple POJOs annotated to tell the jersey framework about different properties such as path, consumes, produces etc. When you return from your service method, jersey takes care of marshalling those objects in the defined 'PRODUCES' responseType and write it on the client stream. Here is a sample of jersey config in web.xml

    <servlet>
            <servlet-name>REST</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>
                    com.rest.services;
                </param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>REST</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    
    0 讨论(0)
提交回复
热议问题