Trying to create a REst Service using Jersey

前端 未结 2 1389
萌比男神i
萌比男神i 2021-02-03 12:43

I am following this tutorial to create a REst Service using Jersey.

Sometimes i fail to understand fully what the author of the tutorial means but these are the steps th

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

    The property com.sun.jersey.config.property.package just needs to be set as the package that contains the web service classes. In the tutorial it is de.vogella.jersey.first, and you can see that the Hello class is declared under that package.

    In other words, when you deploy the application, Jersey will look for web service classes in the package de.vogella.jersey.first, and in this case it will find the class Hello being declared with the javax.ws.rs.Path annotation, and create a web service endpoint listening on the URL that has been declared with @Path.

    However, I have never set such a thing for my Jersey projects. I just put my web service classes in the src folder, and Jersey recognizes them no matter which package I put them inside. This is the minimum configuration that I have with Jersey projects in web.xml:

    <servlet>
        <description>JAX-RS Tools Generated - Do not modify</description>
        <servlet-name>JAX-RS Servlet</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.your.webservice.classes</param-value>
        </init-param> 
        -->
    
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>JAX-RS Servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    

    Also if you do not fancy Maven projects, just create a simple Dynamic Web Project and copy the Jersey JARs to WebContent/WEB-INF/lib.

    Also, as Qwerky suggested, web.xml has to be in WebContent/WEB-INF/ and .jar files should be copied to WebContent/WEB-INF/lib.

    Other than that, the described procedure looks fine!

    0 讨论(0)
  • 2021-02-03 13:31

    For information if you are using Jersey 2 this class has been replaced with jersey.config.server.provider.packages so your resource configuration would be like:

    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>de.vogella.jersey.todo.resources</param-value>
    </init-param>
    
    0 讨论(0)
提交回复
热议问题