Java Jersey rest api com.sun.Jersey.api.container.ContainerExceptionServlet
我对创建API是新手。我在网上找到了一个我认为很简单的例子,但却无法让它发挥作用。任何帮助或建议都会很好 这是我的web.xml文件Java Jersey rest api com.sun.Jersey.api.container.ContainerExceptionServlet,java,rest,servlets,jersey,tomcat6,Java,Rest,Servlets,Jersey,Tomcat6,我对创建API是新手。我在网上找到了一个我认为很简单的例子,但却无法让它发挥作用。任何帮助或建议都会很好 这是我的web.xml文件 <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/n
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Hello, World Application</display-name>
<description>
This is a simple web application with a source code organization
based on the recommendations of the Application Developer's Guide.
</description>
<servlet>
<servlet-name>GetEDPInfo</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>mypackage</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>GetEDPInfo</servlet-name>
<url-pattern>/GetEDPInfo</url-pattern>
</servlet-mapping>
我下载了这些jar文件并将它们放在我的WEB-INF/lib目录中
asm-3.1.jar,jersey-core-1.8.jar,jersey-server-1.8.jar
不确定我是否还需要更多,但这些都是我所看到的示例中使用的
我使用以下命令编译了java文件:
javac-source 1.7-target 1.7-bootclasspath/usr/java/jdk1.7.0_80/jre/lib/rt.jar cp.:/var/lib/tomcat6/webapps/sample/WEB-INF/lib/jersey-core-1.8:/var/lib/tomcat6/webapps/sample/WEB-INF/lib/asm-3.1 GetEDPInfo.java
package mypackage;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/GetEDPInfo")
public class GetEDPInfo {
@GET
@Path("/{param}")
public Response getMsg(@PathParam("param") String msg) {
String output = "Welcome"+ msg;
return Response.status(200).entity(output).build();
}
}
创建我的类文件时没有错误
当我导航到
我收到这个消息
HTTP Status 405 - Method Not Allowed
type Status report
message Method Not Allowed
description The specified HTTP method is not allowed for the requested resource (Method Not Allowed).
HTTP Status 404 - /sample/GetEDPInfo/Jeeves
type Status report
message /sample/GetEDPInfo/Jeeves
description The requested resource (/sample/GetEDPInfo/Jeeves) is not available.
当我导航到
我收到这个消息
HTTP Status 405 - Method Not Allowed
type Status report
message Method Not Allowed
description The specified HTTP method is not allowed for the requested resource (Method Not Allowed).
HTTP Status 404 - /sample/GetEDPInfo/Jeeves
type Status report
message /sample/GetEDPInfo/Jeeves
description The requested resource (/sample/GetEDPInfo/Jeeves) is not available.
这是我修改过的代码
package mypackage;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.core.MediaType;
@Path("/GetEDPInfo")
public class GetEDPInfo {
@GET
@Path("/{param}")
@Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.TEXT_PLAIN)
public String getMsg(@PathParam("param") String msg) {
String output = "Welcome"+ msg;
return output;
}
}将
mypackage.GetEDPInfo
更改为以下内容,它应该可以工作mypackage我能够让它工作,出于某种原因,我需要将web.xml文件更改为使用/*作为我的url模式
<servlet-mapping>
<servlet-name>GetEDPInfo</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
GetEDPInfo
/*
感谢您的回复,我尝试了您的建议,但这给了我一条状态405方法不允许的错误消息。我的url是请尝试此url并共享您的输出grv4:8080/sample/GetEDPInfo/jeeves
,因为根据您的java类,没有具有默认GET实现的方法。我更新了我的帖子以显示正确的“mypackage”您是否可以尝试以下方法并检查其返回的结果是否正确@get
@products(MediaType.TEXT_PLAIN)
@Consumes(MediaType.TEXT_PLAIN)
公共字符串getMsg(@PathParam(“param”)字符串msg){
String output=“Welcome”+msg;
返回输出;
}
我发布了更新的java文件。我以与以前相同的方式编译它,然后重新启动tomcat。导航到url时仍会收到与以前相同的错误消息。如果你还有其他想法,请告诉我,谢谢