Simple Rest Webservice returning http 404 status

I tried to get this tutorial to work: Link I am using Apache Tomcat 7.0 and the Jersey 2.0 library. This is my service:

package org.arpit.javapostsforlearning.webservice; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("ConversionService") public class FeetToInchAndInchToFeetConversionService { @GET @Path("/InchToFeet/{i}") @Produces(MediaType.TEXT_XML) public String convertInchToFeet(@PathParam("i") int i) { int inch=i; double feet = 0; feet =(double) inch/12; return "<InchToFeetService>" + "<Inch>" + inch + "</Inch>" + "<Feet>" + feet + "</Feet>" + "</InchToFeetService>"; } @Path("/FeetToInch/{f}") @GET @Produces(MediaType.TEXT_XML) public String convertFeetToInch(@PathParam("f") int f) { int inch=0; int feet = f; inch = 12*feet; return "<FeetToInchService>" + "<Feet>" + feet + "</Feet>" + "<Inch>" + inch + "</Inch>" + "</FeetToInchService>"; } } 

and this is my web.xml:

 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>RESTfulWebServiceExample</display-name> <servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>org.arpit.javapostsforlearning.webservice</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> </web-app> 

I tried to run it on the server for deployment, and I also tried to let eclipse export it as a war file, and then deploy it using tomcat application manager. In both cases, I get HTTP status 404, the requested resource is unavailable. Prior to this, an error message appears in any logs. I also tried putting a simple index.html file in the Webcontent folder, but I also could not access this in the browser. I know that there are many similar posts on the forum, but after reading them and hours of trying, I still canโ€™t figure out how to solve my problem.

+6
source share
2 answers

Troubleshooting java webservices is sometimes frustrating, so itโ€™s useful to follow some simple steps to find the error.

  • Is your firewall enabled? Make sure it is disabled or your port on your web server is not blocked. In most cases, this is port 8080. It is located in the url url: 8080 / mywebservice / url

  • Check the root application page provided by your server to make sure the server is working at all. On Tomcat, which is usually localhost: 8080 / manager / html

  • Check the logs of your application container for stacktrace at startup to make sure your deployment is not an error.

  • Try deploying the smallest possible web service, such as "Hello World" jax-rs , to make sure you have the right libraries and other configurations available.

  • If you pass all these and still not webservice, your jax-rs annotations are probably in some way wrong. You can include a compliance request in your web.xml by adding to the configuration.

    <web-app> <servlet> <servlet-name>Jersey REST Service for value codes</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> ... <init-param> <param-name>com.sun.jersey.config.feature.Trace</param-name> <param-value>true</param-value> </init-param> </servlet> ... </web-app>

In your journal

You will see a trace of the url.
 {X-Jersey-Trace-008=[mapped exception to response: javax.ws.rs.WebApplicationException@56f9659d -> 415 (Unsupported Media Type)], X-Jersey-Trace-002=[accept right hand path java.util.regex.Matcher[pattern=/myResource/([-0-9a-zA-Z_]+)(/.*)? region=0,17 lastmatch=/myResource/23/mySubresources]: "/myResource/23/mySubresources" -> "/myResource/23" : "/mySubresources"], 

hint: it is probably not recommended to leave this in production

+5
source

Turn on spring logging. It will print all @RequestMapping at startup, as shown below

INFO 2017-09-21 10: 41: 35,822 (AbstractHandlerMethodMapping.javaืœ31) - Mapped "{[/ myapi / cal / calsalary], methods = [GET || PUT], params = [configName && & & &,, configUserId & userId && asId]} "to public

0
source

All Articles