user2322769
user2322769

Reputation:

Trying to create a REst Service using Jersey

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 that i have followed so far :

1) Created a dynamic web project and named it : de.vogella.jersey.first

2) Installed Maven dependencies on eclipse

3) Converted my project to a Maven project (that means created a pom.xml file)

4) Added the necessary dependencies in pom.xml so that i can use jersey without having to manually add the jar files. I added the following xml :

<dependencies>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.17.1</version>
    </dependency>
</dependencies>

5) The author suggests to create a java class and gives some code. I can only assume that he wants us to create a new package in the src folder , name it de.vogella.jersey.first and then create a java class and name it Hello and place the code there. Thats what i did.

6) Then he suggests to open the web.xml file. Theres not such a file in the project though. So i go ahead and create such a file in the WebContent/WEB-INF/lib path. I place the code that he suggest.

7) Next is the step that i fail to understand most. He talks about the web.xml that we just added and more specifically he states:

"The parameter "com.sun.jersey.config.property.package" defines in which package jersey will look for the web service classes. This property must point to your resources classes. "

8) Last step is open the URL http://localhost:8080/de.vogella.jersey.first/rest/hello in my browser. However i get HTTP Status 404 - /de.vogella.jersey.first/rest/hello


With what shall i replace exactly the com.sun.jersey.config.property.package ?

Are the steps that i have followed till now correct , or i misinterpreted something?

Upvotes: 9

Views: 21786

Answers (2)

leldo
leldo

Reputation: 386

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>

Upvotes: 13

Daniel Szalay
Daniel Szalay

Reputation: 4101

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!

Upvotes: 12

Related Questions