Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey, Tomcat: The requested resource is not available error

I have been working towards getting a RESTful service set up using Jersey and Tomcat in RAD 8.5. I have looked at tons of stackoverflow questions related to my error with none of them working. There are no errors in my console.

When I just type: http://localhost:8080/, I get the Apache homepage, so the server is working, but http://localhost:8080/jersey/rest/hello or http://localhost:8080/jersey/WEB-INF/classes/jersey/Hello.java does not work.

Here is the error: (with my library of jars on the side) Here is my Hello.java

package jersey;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class Hello {

      // This method is called if TEXT_PLAIN is request
      @GET
      @Produces(MediaType.TEXT_PLAIN)
      public String sayPlainTextHello() {
        return "Hello Jersey";
      }

      // This method is called if XML is request
      @GET
      @Produces(MediaType.TEXT_XML)
      public String sayXMLHello() {
        return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
      }

      @GET
      @Produces(MediaType.TEXT_HTML)
      public String sayHtmlHello() {
        return "<html> " + "<title>" + "Hello Jersey" + "</title>"
            + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
      }
}

And my web.xml

<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>com.example</param-value>
    </init-param>
</servlet>

Versions:

  • Tomcat: 7.0.663
  • RAD: 8.5
  • Jersey: 2.19

Thanks,

In Response to Maciej This worked! I needed to add <servlet-mapping> with url pattern of /*. Then use http://localhost:8080/jersey/hello, I got a response from the server!

<?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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="jersey" version="2.5">
    <servlet>
        <servlet-name>jersey</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>jersey</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>
like image 728
Jake Schuurmans Avatar asked Aug 12 '15 18:08

Jake Schuurmans


1 Answers

You are deploying a compiled code to Tomcat, so you won't be able to access the *.java resources.

Annotation @Path("/hello") indicates the path at which resource is available.

It is set to: base URL + /your_path. The base URL is based on your application name, the servlet and the URL pattern from the 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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="jersey" version="2.5">
    <servlet>
        <servlet-name>jersey</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>jersey</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

Also replace @Produces annotation to @Consumes:

package jersey;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class Hello {

      // This method is called if TEXT_PLAIN is request
      @GET
      @Consumes(MediaType.TEXT_PLAIN)
      public String sayPlainTextHello() {
        return "Hello Jersey";
      }

      // This method is called if XML is request
      @GET
      @Consumes(MediaType.TEXT_XML)
      public String sayXMLHello() {
        return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
      }

      @GET
      @Consumes(MediaType.TEXT_HTML)
      public String sayHtmlHello() {
        return "<html> " + "<title>" + "Hello Jersey" + "</title>"
            + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
      }
}

Try: http://localhost:8080/jersey/hello

like image 93
Maciej Lach Avatar answered Oct 18 '22 21:10

Maciej Lach