Rest Web服务404错误 - Tomcat Eclipse

时间:2014-11-07 13:44:57

标签: java eclipse web-services rest tomcat

这是我第一次使用Web服务,这让我疯狂。

我正在遵循以下教程:http://www.vogella.com/articles/REST/article.html - 第6节

我创建了Project name作为example.restservice.blue,在其中我有一个包example.restservice.blue,在其中我有一个类嘿:

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

}

以上是资源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>

上面是web.xml文件。

该项目也被命名为example.restservice.blue。

类文件存在于example.restservice.blue/build/classes/example/restservice/blue/中 web.xml文件的存在是example.restservice.blue/WebContent/WEB-INF /

Tomcat 7与另一个应用程序运行良好,并且还显示了tomcat欢迎页面,所以我认为没有任何问题。

这是我得到的错误:

Error 404

这是控制台:

&GT;

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

2 个答案:

答案 0 :(得分:1)

您的配置似乎相当不错,但您无法在浏览器中访问正确的页面:如果您在教程中注意,请访问/ rest / hello页面而不是WEB-INF / classes / hello.java

在Java(或J2EE)中,您永远不会访问Java类:您将访问映射到java控制器(或您的Web服务)的页面。

因此,要从浏览器访问您的网络服务,请将您的网址更正为http://localhost:8080/example.restservice.blue/rest/Hey (对于带注释@Path的Web服务(&#34; /嘿&#34;))

答案 1 :(得分:1)

在成功部署使用JAX-RS开发的Web服务RESTful之前,必须为应用程序配置REST资源路径。有两种方法可以做到这一点:

  • 使用web.xml部署描述符
  • 开发一个扩展javax.ws.rs.core.Application并指定注释@ApplicationPath
  • 的类

使用部署描述符的示例。

网络服务类:

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

部署描述符:

<?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>

在这种情况下,您必须提出此请求:

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

您可以使用包扫描。正如网站上提到的vogella.com

  

参数com.sun.jersey.config.property.package定义在哪个中   包Jersey将查找Web服务类。这个性质   必须指向您的资源类。 URL模式定义了该部分   您的应用程序将放置的基本URL。

在您的情况下,您需要在Path注释中指定正确的URI并提出正确的请求。

另见: