Trying to create a REst service using Jersey

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

Sometimes I don’t completely understand what the author of the textbook means, but these are the steps that I have performed 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 into a Maven project (this means that you created the pom.xml file)

4) Added necessary dependencies in pom.xml so that I can use jersey without having to manually add 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 creating a java class and providing 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 put the code there. Here is what I did.

6) Then he offers to open the web.xml . However, there is no such file in the project. Therefore, I continue and create such a file in the path WebContent/WEB-INF/lib . I am posting the code that it offers.

7) The next step, which I do not understand the most. It talks about the web.xml we just added, and more specifically it states:

The parameter "com.sun.jersey.config.property.package" determines in which package the knitwear will search for web service classes. This property should point to your resource classes. "

8) The last step opens 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


What should I replace com.sun.jersey.config.property.package with exactly?

Are the steps that I have performed so far correct, or am I misinterpreting something?

+7
source share
2 answers

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

In other words, when you deploy the application, Jersey will look for web service classes in the de.vogella.jersey.first package, in which case it will declare the Hello class using the javax.ws.rs.Path annotation and create the web service endpoint listening on the URL that was declared using @Path .

However, I never installed such a thing for projects in Jersey. I just put the web service classes in the src folder and Jersey recognizes them no matter which package I put them in. This is the minimal 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 don't like Maven projects, just create a simple Dynamic Web Project and copy the Jersey JAR to WebContent/WEB-INF/lib .

Like Qwerky , web.xml should be in WebContent/WEB-INF/ and .jar files should be copied to WebContent/WEB-INF/lib .

In addition, the described procedure looks normal!

+11
source

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

 <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>de.vogella.jersey.todo.resources</param-value> </init-param> 
+9
source

All Articles