簡體   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