Jersey Welcome World gives 404

I have the following code in a java class

import javax.ws.rs.*; import javax.ws.rs.core.MediaType; @Path("/hello") public class Hello { //This method is called is TEXT_PLAIN is request @GET @Produces(MediaType.TEXT_PLAIN) public String sayPlainTextHello(){ return "Hello World"; } //this method is called if TEXT_XML is requested @GET @Produces(MediaType.TEXT_XML) public String sayXMLHello(){ return "<?xml version=\"1.0\"?>"+"<Hello> Hello World"+"</hello>"; } //this method is called if HTML is requested @GET @Produces(MediaType.TEXT_HTML) public String sayHtmlHello(){ return "<html>"+"<title>"+"hello jersey"+"</title>"+"<body><h1>"+"hello World!!"+"</body></h1>"+"</html>"; } } 

I compiled it and exported it as a .WAR file when I print

http://127.0.0.1/test_server/hello

I get 404. I tried this in WTP, cURL they all return 404 .. I use tomcat 7.0.26

Note. I am running Tomcat on port 80, and other servlets respond as expected.

Web.xml configuration

 <display-name>Jersey_Test</display-name> <servlet> <servlet-name>Hello</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.example.service</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/*</url-pattern> 

The following URL gives me Http 500 status

  http://localhost/Jersey_Test/rest/hello java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer 
+4
source share
4 answers

The problem is fixed, and so I did.

I deleted the jersey.jar files from the build path and replaced them in the WEB-INF\lib folder and everything worked.

+1
source

This can happen if you have not registered the JAX-RS servlet implementation in your web.xml . Jersey requires the following configuration:

 <servlet> <servlet-name>jersey</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.example.service</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>jersey</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> 

The initialization parameter com.sun.jersey.config.property.packages should point to the package where all your services are located. However, your code snippet is missing a package declaration. I'm not sure if this is omitted for brevity or not, but classes without packaging are not visible to classes that themselves are in the package (for example, the Tomcat and Jersey models themselves). The above web.xml example assumes that you have

 package com.example.service; 

in your webservice classes. Correct or modify it accordingly.

Please note that the URL pattern /* means that ALL requests will be submitted through Jersey. If you need to deploy other servlets, JSPs, or static content in the same webapp, you can specify a more specific URL pattern. For instance.

 <url-pattern>/rest/*</url-pattern> 

You will need to change the URL of your request http: // localhost / test_server / rest / hello .

+7
source

It looks like you are registering your servlet elsewhere. Double check that the root URL of your servlet is and make sure it matches what you click.

Have you tried to get there ?:

 http://127.0.0.1/hello 

Remember that /hello will follow any base URL of your servlet. Try to find it in the debugger to see where it is mounted.

0
source

For me, a jar file was added to the Build path -> Libraries , for which the actual jar missing from the file system.

I removed the entry from the build path and added the dependency in pom.xml . worked like a charm.

0
source

Source: https://habr.com/ru/post/1411573/


All Articles