【问题标题】:Simple RESTtEasy example doesn't work简单的 RESTtEasy 示例不起作用
【发布时间】:2015-11-23 00:36:00
【问题描述】:

我尝试编写一个简单的 RESTEasy 示例来看看它是如何工作的。我在这里找到了信息: http://www.mkyong.com/webservices/jax-rs/integrate-jackson-with-resteasy/ http://www.mkyong.com/webservices/jax-rs/resteasy-hello-world-example/

这真的很简单,我从另一个类似且运行良好的 Restful 示例中理解了它的工作原理。

@Path("/person")
public class PersonResource {
    private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(PersonResource.class);

    private final static String FIRST_NAME = "firstName";
    private final static String LAST_NAME = "lastName";
    private final static String EMAIL = "email";

    private Person person = new Person(1, "Sample", "Person", "sample_person@jerseyrest.com");

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String respondAsReady() {
        return "Entered PersonResource";
    }

    @GET
    @Path("/get")
    @Produces("application/json")
    public Person getProductInJSON() {
        return person;
    }

    @GET
    @Path("sample")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getSamplePerson() {
        LOG.debug("getSamplePerson()");
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("Person First Name:", person.getFirstName());
            jsonObject.put("Person Last Name:", person.getLastName());
        } catch (JSONException e) {
            LOG.debug("jsonObect.put failed");
        }

        String result = "jsonObject:" + jsonObject;
        return Response.status(200).entity(result).build();
    }
}

我的 web.xml:

<context-param>
    <param-name>resteasy.resources</param-name>
    <param-value>com.restexample.PersonResource</param-value>
</context-param>

<listener>
    <listener-class>
        org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
    </listener-class>
</listener>

<servlet>
    <servlet-name>resteasy-servlet</servlet-name>
    <servlet-class>
        org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
    </servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>resteasy-servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

我的 pom.xml:

<!-- RESTEasy-->
<repositories>
    <repository>
        <id>JBoss repository</id>
        <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
    </repository>
</repositories>


<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxrs</artifactId>
        <version>3.0.11.Final</version>
    </dependency>

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson-provider</artifactId>
        <version>3.0.11.Final</version>
    </dependency>
</dependencies>

当我尝试使用http://localhost:8080/rest/person/sample 或任何其他途径来访问这些方法时,会出现一个空白屏幕。我没有 404 NOT FOUND!只是黑屏。 (我正在使用汤姆猫)。谁能帮帮我?

【问题讨论】:

  • 好吧,就像在那些示例中,我希望在浏览器中打印信息。如果我尝试 [localhost:8080/rest/person] 我希望打印“输入的 PersonResource”,我在服务器日志中没有任何异常。
  • 您的调试日志消息是否出现?您是否使用调试器单步执行您的程序?
  • 我的调试信息没有出现在日志文件中
  • 您的应用程序是否设置为 Tomcat 的根上下文?看起来您正在跳过应用程序上下文并直接进入 /rest。
  • 默认链接是 [localhost:8080/] 所以我猜 url 没问题。当我访问 url(调试器)时,永远不会调用该方法。这个带有球衣的例子工作得很好 [avilyne.com/?p=105]

标签: java resteasy


【解决方案1】:

我发现了问题所在。在我的 pom.xml 我有这个:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

此侦听器正在创建应用程序上下文。直到现在都没有错。 在 web.xml 中定义的 ResteasyBootstrap 也提供了一个上下文:

<listener>
    <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>

因为两个侦听器都提供不同的上下文,所以 URL 不会返回 404 NOT FOUND,也不会返回良好的结果。

解决办法:

删除xml中的监听器ContextLoaderListener,一切正常。

【讨论】:

    【解决方案2】:

    尝试在你的 web.xml 中添加这个:

    <context-param>
        <param-name>resteasy.servlet.mapping.prefix</param-name>
        <param-value>/rest</param-value>
    </context-param>
    

    https://docs.jboss.org/resteasy/docs/1.0.1.GA/userguide/html/Installation_Configuration.html

    【讨论】:

    • 那不是原因:(
    【解决方案3】:

    试试这个:

    @GET
    @Path("sample")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getSamplePerson() {
        LOG.debug("getSamplePerson()");
        return Response.status(Status.OK).entity(person).build();
    }
    

    【讨论】:

    • 它不起作用:(还有一个奇怪的事情是我的日志文件是空的。即使我使用记录器,LOG.debug 也不会在日志文件中产生任何输出
    • 尝试调试您的 Tomcat 并在 getSamplePerson() 中设置断点以查看在您打开 URL 时它是否被调用。
    猜你喜欢
    • 1970-01-01
    • 2012-07-01
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多