Getting 404 Error to Run Jersey REST

I am trying to start my first HelloWorld Jersey project by reading a bunch of tutorials on this, and I think that this should work theoretically, but of course I am doing something wrong, that the page gives me a 404 error. Here is what I have:

I started with DynamicWebProject in Eclise and using plugins translated it into a Maven project. And added them to the POM file:

<dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.8</version> </dependency> </dependencies> 

Then I also added a pretty small class, like this one, to have annotations from Jersey:

 import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; @Path("/hello") public class HelloWorldResource { @GET @Produces("application/plain") public String getMessage() { // Forward request to service layer. return "Hello World"; } } 

and I also registered jersey with them in the web.XML file:

  <servlet> <servlet-name>Jersey REST Service</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>JerseyREST</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey REST Service</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> 

NOTE: there were already some created ones, I have not touched them yet. They are still there.

So, with this configuration, I made Run On Server and went to

http://localhost:8080/JerseyREST/rest/hello

but getting the unpleasant HTTP status 404 - / JerseyREST / jerseyrest / rest / hello error on this. And I can’t understand what part I am doing wrong. Any suggestions or places I'm looking at?

Great importance.

+3
source share
2 answers

I used this link to implement my first Jersey: REST web service in Java. I run it on Tomcat v7.0 and it works great. Have you tried it on Tomcat? If not, I suggest you try. Sometimes it happened to me that I got a 404 error forever. To fix the error, I uninstalled Tomcat and created a new server wizard, and then it works fine.

As @Tom said, it can refer to "application/plain" . Use MediaType.TEXT_PLAIN .

You need to add these jar files to / WEB -INF / lib /: asm-3.1 , jackson-core-asl-1.9.2 , jackson-jaxrs-1.9.2 , jackson-mapper-asl-1.9.2 , jackson-xc-1.9.2 , jersey-client-1.11 , jersey-core-1.11 , jersey-json-1.11 , jersey-server-1.11 , jersey-servlet-1.11 , jettison-1.1 and jsr311-api-1.1.1 .

+1
source
  <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>JerseyREST</param-value> 

the parameter value should point to the folder in which you created the class.

0
source

All Articles