【问题标题】:Jersey rest api com.sun.jersey.api.container.ContainerExceptionServletJersey rest api com.sun.jersey.api.container.ContainerExceptionServlet
【发布时间】:2017-08-01 03:41:39
【问题描述】:

我是创建 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.1 GetEDPInfo.java

这创建了我的类文件,没有错误。

当我导航到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;
}

}

【问题讨论】:

    标签: java rest servlets jersey tomcat6


    【解决方案1】:

    &lt;param-value&gt;mypackage.GetEDPInfo&lt;/param-value&gt; 更改为关注,它应该可以工作&lt;param-value&gt;mypackage&lt;/param-value&gt;

    【讨论】:

    • 感谢您的回复,我尝试了您的建议,但是这给了我一个状态 405 Method not allowed 错误消息。我的网址是grv4:8080/sample/GetEDPInfo
    • 请试试这个 url 并分享你的输出 grv4:8080/sample/GetEDPInfo/jeeves 因为根据你的 java 类没有默认 GET 实现的方法。
    • 我更新了我的帖子以在 xml 文件中显示正确的“mypackage”,并显示了我在访问 url 时收到的错误消息。
    • 你可以试试下面的方法,看看它是否返回正确的结果@GET@Path("/{param}")@Produces(MediaType.TEXT_PLAIN)@Consumes(MediaType.TEXT_PLAIN)public String getMsg(@PathParam("param") String msg) {String output = "Welcome"+ msg;String output = "Welcome"+ msg;return output;}
    • 我发布了更新的 java 文件。我以与以前相同的方式编译它,然后重新启动了tomcat。导航到 url 时仍会收到与以前相同的错误消息。如果您有任何其他想法,请告诉我,谢谢
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-16
      • 2014-11-15
      • 1970-01-01
      • 2016-01-27
      • 1970-01-01
      • 1970-01-01
      • 2018-10-23
      • 2016-02-06
      相关资源
      最近更新 更多