下面是我的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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>jersey_helloworld</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>jerseyhello</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.javapapers.webservices.rest.jersey</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jerseyhello</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
下面是web-inf / lib中的所有jar,我已将它们添加到构建路径中。
以下是我的HelloWorld代码。
package com.javapapers.webservices.rest.jersey;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/helloworld")
public class HelloWorld {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello World RESTful Jersey!";
}
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello World RESTful Jersey"
+ "</hello>";
}
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello World RESTful Jersey"
+ "</title>" + "<body><h1>" + "Hello World RESTful Jersey"
+ "</body></h1>" + "</html> ";
}
}
最后,我从firefox中调用了这个Web服务,如下所示。
http://localhost:8080/jersey_helloworld/rest/helloworld
然后打印HTTP状态404 - 未找到。并且在eclipse控制台中没有打印任何错误消息。
答案 0 :(得分:1)
我用过:
1)关注罐子:
2)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" 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_2_5.xsd">
<servlet>
<servlet-name>jerseyhello</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.javapapers.webservices.rest.jersey</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jerseyhello</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
3)您的源代码没有任何修改。
结果是:
答案 1 :(得分:0)
您必须为每个方法添加@Path
注释以指定不同的路径。现在,您只调用/helloworld
,但没有与该路径关联的方法。
例如,对于第一种方法,尝试这样做:
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/plainText")
public String sayPlainTextHello() {
return "Hello World RESTful Jersey!";
}
并以这种方式称呼它:
http://localhost:8080/jersey_helloworld/rest/helloworld/plainText