[英]Rest Web services returning a 404
This is my first time using Eclipse, and is causing me to rage a lot. 这是我第一次使用Eclipse,并且让我大发雷霆。
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 我安装了Tomcat 6.0,下载了Jersey库,我按照以下教程: 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: 我创建了项目名称作为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> ";
}
}
and my web.xml looks like 我的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>
ANd I am trying to use curl as: 我试图使用curl作为:
curl http://localhost:8081/RestExample/rest/hello curl http:// localhost:8081 / RestExample / rest / hello
Apache Tomcat/6.0.35 - Error report Apache Tomcat / 6.0.35 - 错误报告
type Status re port
类型状态重新端口
message /RestExample/rest/hello
message / RestExample / rest / hello
de scription The requested resource (/RestExample/rest/hello) is not available.
德scription所请求的资源(/ RestExample / REST /你好)不可用。
The question is what should I change in the web.xml so that I can access that resource? 问题是我应该在web.xml中更改什么才能访问该资源?
I tried RestExample/de.jay.jersey.first/rest/hello, and it still did not work. 我试过RestExample / de.jay.jersey.first / rest / hello,但它仍然没有用。 TOmcat is running without errors.
TOmcat运行没有错误。
It took me a lot of time to figure out why it wasn't working for me inspite of looking all over the web for solution. 我花了很多时间来弄清楚为什么它不适合我,尽管在网上寻找解决方案。 The mistake I was making was that package names were not up to date to the new jersey api.
我犯的错误是包裹名称与新球衣api不同。 Here's what updated package names should look like (Web.xml):
这是更新的包名称应该是什么样的(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>
Notice that <servlet-class>
and <param-name>
are different(updated) from vogella tutorial . 请注意,
<servlet-class>
和<param-name>
与vogella 教程不同(更新)。 It may not be the answer to this particular question but might help someone. 它可能不是这个特定问题的答案,但可能对某些人有帮助。 I found it from here .
我从这里找到了它。
Please add all the given Jars in your project 请在项目中添加所有给定的Jars
Project (Right Click)>Properties>Java Build Path>Libraries>Add JARs/Add External JARs 项目(右键单击)>属性> Java构建路径>库>添加JAR /添加外部JAR
I tried it with Tomcat 7.0 and it works fine: 我尝试使用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 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 浏览到http:// localhost:8084 / RestExample / rest / hello ,它运行正常
i looked for a solution to the same problem for hours too. 我也在寻找解决同样问题的解决方案。
this solved my problem: 这解决了我的问题:
if you use a Maven-Project (for example with archetype maven-archetype-webapp) and the class HelloWorldResource is implemented in the folder src/main/resources this class doesn't get compiled (for example then running "mvn clean package" or "run on server" in eclipse) 如果您使用Maven-Project(例如使用archetype maven-archetype-webapp)并且在文件src / main / resources中实现类HelloWorldResource,则此类不会被编译(例如,然后运行“mvn clean package”或在eclipse中“在服务器上运行”
Implement HelloWorldResource in folder src/main/java instead and no more 404 occures.. (if you create Maven-Project with maven-archetype-webapp the folder needs to be manually created) 在文件夹src / main / java中实现HelloWorldResource而不再发生404 ..(如果使用maven-archetype-webapp创建Maven-Project,则需要手动创建文件夹)
检查你的路径是否有这个条'/'示例:@Path('/ path')在某些情况下这个问题只是缺少的吧!
If you are using Jersey 2.XX user ServletAdaptor instead. 如果您正在使用Jersey 2.XX用户ServletAdaptor。 Like this,
像这样,
<servlet-class>com.sun.jersey.server.impl.container.servlet.ServletAdaptor</servlet-class>
<!-- <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> -->
like the aticle says: 像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));
you try to specify the method path which you want to call 您尝试指定要调用的方法路径
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.