请求的资源不可用错误:Rest Jersey(Eclipse)

时间:2017-08-29 23:20:45

标签: java eclipse rest tomcat jersey-2.0

我一直在尝试使用jersey,tomcat和eclipse处理Rest应用程序,但我收到404错误(请求的资源不可用。)我确实经历了所有类似的错误,但我无法解决这个问题。

这是我的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.kushal.rest.testRest</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>

我交叉检查了servlet名称并且它是匹配的。 我尝试了jdk 1.7和tomcat 7以及jdk 1.8和tomcat 8以及不同版本的jersey我最近尝试的是2.25。

这是我的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>org.kushal.rest</groupId>
    <artifactId>testRest</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>testRest</name>

    <build>
        <finalName>testRest</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <!-- uncomment this to get JSON support
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>
        -->
    </dependencies>
    <properties>
        <jersey.version>2.25</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

这是我的resource.java文件

package org.kushal.rest.testRest;

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!";
    }
}

http://localhost:8080/testRest/myresource http://localhost:8080/testRest 请让我知道我做错了什么,因为我在过去的3天里被困住了。

here are the logs:

Aug 29, 2017 5:11:03 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:messenger' did not find a matching property.
Aug 29, 2017 5:11:03 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/8.0.39
Aug 29, 2017 5:11:03 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Nov 9 2016 08:48:39 UTC
Aug 29, 2017 5:11:03 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         8.0.39.0
Aug 29, 2017 5:11:03 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 10
Aug 29, 2017 5:11:03 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            10.0
Aug 29, 2017 5:11:03 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Aug 29, 2017 5:11:03 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jre1.8.0_91
Aug 29, 2017 5:11:03 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_91-b15
Aug 29, 2017 5:11:03 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Aug 29, 2017 5:11:03 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         C:\Users\kushaldileep\New_Rest\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Aug 29, 2017 5:11:03 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:\Courses\273\apache-tomcat-8.0.39
Aug 29, 2017 5:11:03 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\Users\kushaldileep\New_Rest\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Aug 29, 2017 5:11:03 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\Courses\273\apache-tomcat-8.0.39
Aug 29, 2017 5:11:03 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\Users\kushaldileep\New_Rest\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Aug 29, 2017 5:11:03 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\Courses\273\apache-tomcat-8.0.39\endorsed
Aug 29, 2017 5:11:03 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Aug 29, 2017 5:11:03 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre1.8.0_91\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_91/bin/server;C:/Program Files/Java/jre1.8.0_91/bin;C:/Program Files/Java/jre1.8.0_91/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\CodeBlocks\MinGW\bin;C:\Program Files (x86)\Brackets\command;C:\Program Files\Java\jdk1.8.0_65\bin;C:\Program Files (x86)\GtkSharp\2.12\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\Program Files (x86)\Skype\Phone\;C:\Program Files\nodejs\;C:\Program Files\MySQL\MySQL Utilities 1.6\;C:\Users\kushaldileep\AppData\Local\Programs\Python\Python36-32\Scripts\;C:\Users\kushaldileep\AppData\Local\Programs\Python\Python36-32\;C:\Users\kushaldileep\Anaconda2;C:\Users\kushaldileep\Anaconda2\Scripts;C:\Users\kushaldileep\Anaconda2\Library\bin;C:\Users\kushaldileep\AppData\Roaming\npm;C:\Users\kushaldileep\AppData\Local\Microsoft\WindowsApps;;C:\Courses\273\Enide-2015-7-windows-x64-20150706\eclipse;;.
Aug 29, 2017 5:11:03 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Aug 29, 2017 5:11:03 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Aug 29, 2017 5:11:03 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Aug 29, 2017 5:11:03 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Aug 29, 2017 5:11:03 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1636 ms
Aug 29, 2017 5:11:04 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Aug 29, 2017 5:11:04 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.39
Aug 29, 2017 5:11:04 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [101] milliseconds.
Aug 29, 2017 5:11:04 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Aug 29, 2017 5:11:04 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Aug 29, 2017 5:11:04 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 901 ms

2 个答案:

答案 0 :(得分:0)

我认为你需要改变你的pom.xml的创建方式,首先我开始使用你的pom.xml,但它给了我同样的错误。

不试试这个,pom.xml是基于maven存储库和依赖项创建的,我可以将其视为项目的必需项。这是一步一步(在这种情况下我使用的是Eclipse Oxygen,Tomcat 9,java 8)

  1. 打开Eclipse
  2. 文件&gt;新&gt;动态Web项目
    2.1项目名称:my-jersey-project
  3. 点击完成
  4. 右键点击my-jersey-project&gt;配置&gt; 转换为Maven 项目
    4.1组ID:org.jersey.demo.project
    4.2点击完成
  5. </build>部分添加此依赖项
  6. 后打开pom.xml

    依赖关系:

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-server -->
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.25.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-common -->
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-common</artifactId>
            <version>2.25.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-servlet -->
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.25.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-servlet-core -->
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>2.25.1</version>
        </dependency>
    </dependencies>
    
    1. 展开my-jersey-project项目并右键单击部署描述符:my-jersey-project &gt;选择生成部署描述符存根

    2. 打开刚刚从上一步创建的web.xml

    3. 这是web.xml的内容

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
        <display-name>my-jersey-project</display-name>
       <servlet>
          <servlet-name>Jersey REST Service</servlet-name>
          <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
           <!-- Register resources and providers under com.vogella.jersey.first package. -->
          <init-param>
              <param-name>jersey.config.server.provider.packages</param-name>
              <param-value>org.jersey.demo.project</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>
      
      1. 转到src文件夹,创建一个名为org.jersey.demo.project的新包

      2. org.jersey.demo.project中创建一个名为Hello的新java类

      3. 它是Hello

        的内容
        package org.jersey.demo.project;
        
        import javax.ws.rs.GET;
        import javax.ws.rs.Path;
        import javax.ws.rs.PathParam;
        import javax.ws.rs.core.Response;
        
        @Path("/hello")
        public class Hello {
        
            @GET
            @Path("/{param}")
            public Response getMsg(@PathParam("param") String msg) {
        
                String output = "Hello Jersey this is the param value: " + msg;
        
                return Response.status(200).entity(output).build();
        
            }
        
        }
        

        此步骤非常重要

        1. 右键点击my-jersey-project&gt; Maven &gt; 更新项目,确保选中my-jersey-project&gt;单击确定,请稍等片刻,直到maven依赖项正确加载到您的项目中。

        2. 右键点击my-jersey-project&gt;以&gt;运行在服务器上运行&gt;选择tomcat服务器(假设已经在你的eclipse上配置了tomcat服务器)&gt;点击完成

        3. 最后尝试一下您的休息服务:http://localhost:8080/my-jersey-project/rest/hello/hijersey

        4. 这是我从测试得到的输出: Hello Jersey这是参数值:hijersey

        5. 希望这些信息对您有所帮助。

答案 1 :(得分:0)

好的,我刚刚构建了你的包并将war文件复制到tomcat webapps目录,这个端点localhost:8080/testRest/webapi/myresource正常工作。以下是关于testRest.war部署的catalina.out片段:

30-Aug-2017 12:21:52.872 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /Users/ssiryk/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
30-Aug-2017 12:21:53.020 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
30-Aug-2017 12:21:53.052 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
30-Aug-2017 12:21:53.056 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8000"]
30-Aug-2017 12:21:53.056 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
30-Aug-2017 12:21:53.057 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["https-jsse-nio-8443"]
30-Aug-2017 12:21:53.310 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
30-Aug-2017 12:21:53.310 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
30-Aug-2017 12:21:53.312 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
30-Aug-2017 12:21:53.313 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 883 ms
30-Aug-2017 12:21:53.342 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
30-Aug-2017 12:21:53.342 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.11
**30-Aug-2017 12:21:53.368 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive /Applications/apache-tomcat-8.5.11/webapps/testRest.war
30-Aug-2017 12:21:54.119 INFO [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
30-Aug-2017 12:21:54.959 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive /Applications/apache-tomcat-8.5.11/webapps/testRest.war has finished in 1,590 ms**
30-Aug-2017 12:21:54.960 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /Applications/apache-tomcat-8.5.11/webapps/docs
30-Aug-2017 12:21:54.972 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /Applications/apache-tomcat-8.5.11/webapps/docs has finished in 12 ms

我标记了有关testRest.war部署的行。 希望这有帮助!