简体   繁体   English

REST Web服务404错误-Tomcat Eclipse

[英]Rest Web service 404 error - Tomcat Eclipse

This is the first time I'm using Web Services, and it's driving me nuts. 这是我第一次使用Web服务,这使我发疯。

I'm following the tutorial from : http://www.vogella.com/articles/REST/article.html - section 6 我正在遵循以下教程: http : //www.vogella.com/articles/REST/article.html-第6节

I created the Project Name as example.restservice.blue, and within that I have a package example.restservice.blue and within that I have a class Hey: 我将项目名称创建为example.restservice.blue,其中有一个example.restservice.blue包,其中有Hey类:

package example.restservice.blue;


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

// Plain old Java Object it does not extend as class or implements 
// an interface

// The class registers its methods for the HTTP GET request using the @GET annotation. 
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML. 

// The browser requests per default the HTML MIME type.

//Sets the path to base URL + /hello
@Path("/Hey")
public class Hey {

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

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

// This method is called if HTML is request
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello Jersey, please please work" + "</title>"
    + "<body><h1>" + "Hello Jersey, please please work" + "</body></h1>" + "</html> ";
}

}

Above is the resources java file. 上面是资源java文件。

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>example.restservice.blue</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<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>example.restservice.blue</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>

Above is the web.xml file. 上面是web.xml文件。

The project is named as example.restservice.blue as well. 该项目也被命名为example.restservice.blue。

The class file is present in example.restservice.blue/build/classes/example/restservice/blue/ The web.xml file is present is example.restservice.blue/WebContent/WEB-INF/ 该类文件位于example.restservice.blue/build/classes/example/restservice/blue/中。web.xml文件位于example.restservice.blue/WebContent/WEB-INF/中

Tomcat 7 runs fine with another application and also displays the tomcat welcome page, so there are no issues with it, I think. Tomcat 7在另一个应用程序上运行良好,并且还显示了tomcat欢迎页面,因此我认为它没有问题。

here is the error i get: 这是我得到的错误:

错误404

Here is the console: 这是控制台:

> >

Nov 07, 2014 5:34:37 PM org.apache.catalina.core.AprLifecycleListener init
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\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files (x86)\QuickTime\QTSystem\;.
Nov 07, 2014 5:34:38 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:example.restservice.bhagya' did not find a matching property.
Nov 07, 2014 5:34:38 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Nov 07, 2014 5:34:38 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Nov 07, 2014 5:34:38 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1608 ms
Nov 07, 2014 5:34:38 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Nov 07, 2014 5:34:38 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
Nov 07, 2014 5:34:38 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(C:\Users\vaio\Desktop\RESTProject\wtpwebapps\example.restservice.blue\WEB-INF\lib\javax.servlet-api-3.0.1.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
Nov 07, 2014 5:34:42 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Users\vaio\Desktop\RESTProject\webapps\docs
Nov 07, 2014 5:34:42 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Users\vaio\Desktop\RESTProject\webapps\examples
Nov 07, 2014 5:34:42 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
Nov 07, 2014 5:34:42 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
Nov 07, 2014 5:34:42 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@53d4287')
Nov 07, 2014 5:34:42 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Users\vaio\Desktop\RESTProject\webapps\host-manager
Nov 07, 2014 5:34:42 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Users\vaio\Desktop\RESTProject\webapps\manager
Nov 07, 2014 5:34:42 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Users\vaio\Desktop\RESTProject\webapps\ROOT
Nov 07, 2014 5:34:42 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Nov 07, 2014 5:34:42 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Nov 07, 2014 5:34:42 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 4642 ms

Your configuration seems quite good but you don't access the correct page in your browser: if you pay attention in your tutorial it access to /rest/hello page not WEB-INF/classes/hello.java 您的配置看起来不错,但是您无法在浏览器中访问正确的页面:如果您在教程中注意它,则访问/ rest / hello页面而不是WEB-INF / classes / hello.java

In Java (or J2EE) you will never access a Java class: you will access a page that is mapped to a java controller (or web service in your case). 在Java(或J2EE)中,您将永远不会访问Java类:您将访问映射到Java控制器(或您的情况下的Web服务)的页面。

So to access your web service from browser, correct your URL to something like http://localhost:8080/example.restservice.blue/rest/Hey (for the web service with annotation @Path("/Hey")) 因此,要从浏览器访问您的Web服务,请将您的URL改成http://localhost:8080/example.restservice.blue/rest/Hey (对于带有批注@Path(“ / Hey”)的Web服务)

Before successfully deploy a web service RESTful, which was developed using JAX-RS, you must configure the REST resource path for your application. 在成功部署使用JAX-RS开发的Web服务RESTful之前,必须为应用程序配置REST资源路径。 There are two ways to do this: 有两种方法可以做到这一点:

  • To use web.xml deployment descriptor 使用web.xml部署描述符
  • Develop a class that extends javax.ws.rs.core.Application and specify annotation @ApplicationPath 开发一个扩展javax.ws.rs.core.Application的类并指定@ApplicationPath注释

Example with using the deployment descriptor. 使用部署描述符的示例。

Class of web- service : 网络服务类别:

package example.restservice.blue;

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

@Path("Hey")
public class Hey {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayPlainTextHello() {
        return "Hello Jersey, please please work";
    }

    @GET
    @Produces(MediaType.TEXT_XML)
    public String sayXMLHello() {
        return "<?xml version=\"1.0\"?>" + 
           "<hello> Hello Jersey, please please work" + "</hello>";
    }
}

Deployment descriptor: 部署描述符:

<?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_3_0.xsd" 
  id="WebApp_ID" 
  version="3.0">

<display-name>example.restservice.blue</display-name>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>JerseyService</servlet-name>
    <servlet-class>
       com.sun.jersey.spi.container.servlet.ServletContainer
    </servlet-class>

  <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>JerseyService</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

</web-app>

In this case you must make this request: 在这种情况下,您必须发出以下请求:

http://localhost:8080/root_context_here/rest/Hey

You can use the package scanning. 您可以使用包裹扫描。 As mentioned on the website vogella.com , 如网站vogella.com所述

The parameter com.sun.jersey.config.property.package defines in which package Jersey will look for the web service classes. 参数com.sun.jersey.config.property.package定义了Jersey将在哪个包中查找Web服务类。 This property must point to your resources classes. 此属性必须指向您的资源类。 The URL pattern defines the part of the base URL your application will be placed. URL模式定义了将放置应用程序的基本URL的一部分。

In your case, you need to specify the correct URI in Path annotation and make the right request. 在您的情况下,您需要在“路径注释”中指定正确的URI并提出正确的请求。

See Also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM