休息Web服务返回404 [英] Rest Web services returning a 404

查看:118
本文介绍了休息Web服务返回404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用Eclipse,并且引起我的愤怒。



我安装了Tomcat 6.0,下载了泽西图书馆,并按照以下教程: 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 {
//如果TEXT_PLAIN请求,则调用此方法
@GET
@Produces (MediaType.TEXT_PLAIN)
public String sayPlainTextHello(){
returnHello Jersey;
}

//如果XML请求,则调用此方法
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello(){
return<?xml version = \1.0\?> +< hello> Hello Jersey+< / hello>;
}

//如果HTML请求
@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.0encoding =UTF-8?> 
< web-app xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns =http://java.sun.com/xml/ns/javaeexmlns :web =http://java.sun.com/xml/ns/javaee/web-app_2_5.xsdxsi:schemaLocation =http://java.sun.com/xml/ns/javaee http:// java.sun.com/xml/ns/javaee/web-app_2_5.xsdid =WebApp_IDversion =2.5>
< display-name> RestExample< / display-name>
< servlet>
< servlet-name> Jersey REST服务< / 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服务< / servlet-name>
< url-pattern> / rest / *< / url-pattern>
< / servlet-mapping>
< / web-app>

我正在尝试使用卷曲:



Apache Tomcat / 6.0.35 - 错误报告

HTTP状态404 - / RestExample / rest / Hello

类型状态
端口

消息 / RestExample / rest / hello

de
scription
请求的资源/ RestExample / rest / hello)是
不可用。

Apache Tomcat / 6.0.35



问题是我应该在网络上改变什么.xml,以便我可以访问该资源?



我尝试了RestExample / de.jay.jersey.first / rest / hello,它仍然没有起作用。 TOMcat正在运行没有错误。

解决方案

我尝试使用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 {
//如果TEXT_PLAIN请求,则调用此方法
@GET
@Produces (MediaType.TEXT_PLAIN)
public String sayPlainTextHello(){
returnHello Jersey;
}

//如果XML请求,则调用此方法
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello(){
return<?xml version = \1.0\?> +< hello> Hello Jersey+< / hello>;
}

//如果HTML请求
@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.0encoding =UTF-8?> 
< web-app version =3.0xmlns =http://java.sun.com/xml/ns/javaeexmlns:xsi =http://www.w3.org/2001/ XMLSchema-instancexsi: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服务< / 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服务< / servlet-name>
< url-pattern> / rest / *< / url-pattern>
< / servlet-mapping>
< / web-app>

浏览到 http:// localhost:8084 / RestExample / rest / hello ,它工作正常


This is my first time using Eclipse, and is causing me to rage a lot.

I installed Tomcat 6.0, downloaded the Jersey libraries, and I followed the tutorials from : http://www.vogella.com/articles/REST/article.html#first_client

I created the Project Name as RestExample, and within that I have a package de.jay.jersey.first and within that I have a class HelloWorldResource, and here is what it looks like:

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

and my web.xml looks like

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

ANd I am trying to use curl as:

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

Apache Tomcat/6.0.35 - Error report

HTTP Status 404 - /RestExample/rest/Hello

type Status re port

message /RestExample/rest/hello

de scription The requested resource (/RestExample/rest/hello) is not available.

Apache Tomcat/6.0.35

The question is what should I change in the web.xml so that I can access that resource?

I tried RestExample/de.jay.jersey.first/rest/hello, and it still did not work. TOmcat is running without errors.

解决方案

I tried it with Tomcat 7.0 and it works fine:

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>

Browsed to http://localhost:8084/RestExample/rest/hello and it works ok

这篇关于休息Web服务返回404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆