球衣休息 api com.sun.jersey.api.container.ContainerExceptionServlet

Jersey rest api com.sun.jersey.api.container.ContainerExceptionServlet

我是 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/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>

这是我的 java 示例 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();
}

}

我下载了这些 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/jersey-server-1.8:/var/lib/tomcat6/webapps/sample /WEB-INF/lib/asm-3.1GetEDPInfo.java

创建我的 class 文件没有错误。

当我导航到 http://grv4:8080/sample/GetEDPInfo 我收到这条消息...

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://grv4:8080/sample/GetEDPInfo/Jeeves 我收到这条消息...

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

}

<param-value>mypackage.GetEDPInfo</param-value> 更改为以下,它应该可以工作 <param-value>mypackage</param-value>

我能够让它工作,出于某种原因我需要更改我的 web.xml 文件以使用 /* 作为我的 url 模式...

<servlet-mapping>
<servlet-name>GetEDPInfo</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>