繁体   English   中英

其余Web服务返回404

[英]Rest Web services returning a 404

这是我第一次使用Eclipse,并且让我大发雷霆。

我安装了Tomcat 6.0,下载了Jersey库,我按照以下教程: http//www.vogella.com/articles/REST/article.html#first_client

我创建了项目名称作为RestExample,并在其中我有一个包de.jay.jersey.first,其中我有一个类HelloWorldResource,这是它的样子:

package de.jay.jersey.first;

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 HelloWorldResource {
// 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>";
}

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

我的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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>RestExample</display-name>
  <servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
  <param-name>com.sun.jersey.config.property.packages</param-name>
  <param-value>de.jay.jersey.first</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>

我试图使用curl作为:

curl http:// localhost:8081 / RestExample / rest / hello

Apache Tomcat / 6.0.35 - 错误报告

HTTP状态404 - / RestExample / rest / Hello

类型状态重新端口

message / RestExample / rest / hello

德scription所请求的资源(/ RestExample / REST /你好)不可用。

Apache Tomcat / 6.0.35

问题是我应该在web.xml中更改什么才能访问该资源?

我试过RestExample / de.jay.jersey.first / rest / hello,但它仍然没有用。 TOmcat运行没有错误。

我花了很多时间来弄清楚为什么它不适合我,尽管在网上寻找解决方案。 我犯的错误是包裹名称与新球衣api不同。 这是更新的包名称应该是什么样的(Web.xml):

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
    <display-name>RestExample</display-name>
    <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>de.jay.jersey.first</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>

请注意, <servlet-class><param-name>与vogella 教程不同(更新)。 它可能不是这个特定问题的答案,但可能对某些人有帮助。 我从这里找到了它。

请在项目中添加所有给定的Jars

项目(右键单击)>属性> Java构建路径>库>添加JAR /添加外部JAR

  1. ASM-3.1.jar
  2. 球衣束,1.14.jar
  3. 球衣-client.jar中
  4. 球衣,core.1.17.1.jar
  5. 新泽西服务器1.17.jar
  6. 新泽西州的servlet-1.17.jar

我尝试使用Tomcat 7.0,它工作正常:

package de.jay.jersey.first;

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 HelloWorldResource {
// 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>";
    }

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

web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
    <display-name>RestExample</display-name>
    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>de.jay.jersey.first</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>

浏览到http:// localhost:8084 / RestExample / rest / hello ,它运行正常

我也在寻找解决同样问题的解决方案。

这解决了我的问题:

如果您使用Maven-Project(例如使用archetype maven-archetype-webapp)并且在文件src / main / resources中实现类HelloWorldResource,则此类不会被编译(例如,然后运行“mvn clean package”或在eclipse中“在服务器上运行”

在文件夹src / main / java中实现HelloWorldResource而不再发生404 ..(如果使用maven-archetype-webapp创建Maven-Project,则需要手动创建文件夹)

检查你的路径是否有这个条'/'示例:@Path('/ path')在某些情况下这个问题只是缺少的吧!

如果您正在使用Jersey 2.XX用户ServletAdaptor。 像这样,

    <servlet-class>com.sun.jersey.server.impl.container.servlet.ServletAdaptor</servlet-class>
    <!-- <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> -->

像aticle说:

// Get the Todos
    System.out.println(service.path("rest").path("todos").accept(
            MediaType.TEXT_XML).get(String.class));
    // Get XML for application
    System.out.println(service.path("rest").path("todos").accept(
            MediaType.APPLICATION_JSON).get(String.class));
    // Get JSON for application
    System.out.println(service.path("rest").path("todos").accept(
            MediaType.APPLICATION_XML).get(String.class));

您尝试指定要调用的方法路径

暂无
暂无

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

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