Jersey: The request resource is not available

2019-08-29 06:56发布

问题:

I am trying to get my first Jersey web service project to work but I am getting this error Jersey: The requested resource is not available. I have installed Jersey 2.16 by eclispe Maven and installed Tomcat 8.0.21 I have craeted the MessageResource.java class inside scr/main/java - org.test.messanger

I am typing this link:

http://localhost:8080/messanger/webapi/messages

If I klick this link http://localhost:8080/messanger/webapi/myresource I am getting Got it!

I added Jersy with maven with this data:

  • org.glassfish.jersey.archetypes
  • jersey-Quickstart-webapp
  • 2.16

MessageResource.java

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


@Path("/messages")
public class MessageResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getMessages(){
        return "Hello world";
    }

}

Working code:

package org.test.messanger;

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

/**
 * Root resource (exposed at "myresource" path)
 */
@Path("myresource")
public class MyResource {

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return "Got it!";
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</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.test.messanger</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
</web-app>

回答1:

Do one thing.

  1. Copy the messenger.jar from target/messenger.jar and paste it inside tomcat folder at location TOMCAT_DIR/tomcat/webapps. TOMCAT_DIR is the directory location for tomcat in your local.

  2. Now go to TOMCAT_DIR/tomcat/bin and start startup.bat or startup.sh.

  3. Now open url http://localhost:8080/messanger/webapi/messages. This should work.

If it worked then go and delete the configured tomcat server from eclipse and configure it one more time.



回答2:

It looks like everything is working just fine, but the messenger.jar was distributed into your Tomcat server's build directory the first time you attempted to host this project.

If you are not properly managing Tomcat through the Manager portal

localhost:port#/manager

by undeploying the project then Tomcat will attempt to rebuild your older project.

Alternatively to using tomcat's Manager GUI, in your Tomcat directory TOMCAT_DIR/tomcat/work delete the folder with the same name as your project and when you revisit the URL Tomcat will build the latest deployment from your TOMCAT_DIR/tomcat/webapps folder.



回答3:

I faced the same issue. The problem with me was that I didn't build my eclipse project and was directly trying to run it on the server.

  • Right-click on your project
  • Click on Build Project
  • Once project is built, run it on the server.