NotFoundException 的 HTTP 状态代码 500

发布于 2024-12-22 04:33:09 字数 403 浏览 2 评论 0原文

我正在使用 RESTEasy 和 JBOSS 5.1 开发一个应用程序。

对于特定情况,我必须返回 404 错误(未找到)。

在源代码中,我正在使用

import org.jboss.resteasy.spi.NotFoundException;
throw new NotFoundException(...);

问题是,在标头响应中,我有 状态代码:500内部服务器错误

即使在正文中异常是:

org.jboss.resteasy.spi.UnhandledException: org.jboss.resteasy.spi.NotFoundException

这是正常行为吗?无法返回状态码:404?

I'm developing an application with RESTEasy and JBOSS 5.1.

For specific situations, I have to return 404 error (not found).

In the sources, I'm using

import org.jboss.resteasy.spi.NotFoundException;
throw new NotFoundException(...);

The problem is that, in the header response, I have
Status Code: 500 internal server error

even if in the body the exception is:

org.jboss.resteasy.spi.UnhandledException: org.jboss.resteasy.spi.NotFoundException

This is a normal behavior? It's not possible to return Status Code: 404?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秋叶绚丽 2024-12-29 04:33:09

我遇到一些问题。我找到了根本原因。内置异常处理仅出现在resteasy最新版本build 2.3.1 GA中。如果你升级到这个版本。你可以得到预期的结果。

I encounter some problem. I found the root cause. The built-in exception handle is only occur in resteasy newest version build 2.3.1 GA. If you upgrade to this version.You can get the expected result.

清泪尽 2024-12-29 04:33:09

RestEASY 没有立即处理 NotFoundException ,这似乎有点奇怪。它应该,根据文档:< /a>

Resteasy 有一组内置异常,当在调度或编组过程中遇到错误时会抛出这些异常。

无论如何,您可以通过添加 ExceptionMapper 来解决这个问题:

import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.mock.MockDispatcherFactory;
import org.jboss.resteasy.mock.MockHttpRequest;
import org.jboss.resteasy.mock.MockHttpResponse;
import org.jboss.resteasy.spi.NotFoundException;
import org.junit.Assert;
import org.junit.Test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;

public class ExceptionTest {

    @Path("/")
    public static class Service {
        @GET
        public String notFound() throws NotFoundException {
            throw new NotFoundException("");
        }
    }

    public static class FailureExceptionMapper implements ExceptionMapper<NotFoundException> {

        @Override
        public Response toResponse(NotFoundException exception) {
            return Response.status(exception.getErrorCode()).build();
        }
    }

    @Test
    public void test() throws Exception {
        Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
        dispatcher.getProviderFactory().addExceptionMapper(new FailureExceptionMapper());
        dispatcher.getRegistry().addSingletonResource(new Service());


        MockHttpRequest request = MockHttpRequest.get("/");
        MockHttpResponse response = new MockHttpResponse();

        dispatcher.invoke(request, response);

        Assert.assertEquals(404, response.getStatus());


    }
}

It does seem a bit strange that RestEASY does not handle the NotFoundException out of the box. It should, according to the docs:

Resteasy has a set of built-in exceptions that are thrown by it when it encounters errors during dispatching or marshalling.

Anyways, you can work around it by adding an ExceptionMapper:

import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.mock.MockDispatcherFactory;
import org.jboss.resteasy.mock.MockHttpRequest;
import org.jboss.resteasy.mock.MockHttpResponse;
import org.jboss.resteasy.spi.NotFoundException;
import org.junit.Assert;
import org.junit.Test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;

public class ExceptionTest {

    @Path("/")
    public static class Service {
        @GET
        public String notFound() throws NotFoundException {
            throw new NotFoundException("");
        }
    }

    public static class FailureExceptionMapper implements ExceptionMapper<NotFoundException> {

        @Override
        public Response toResponse(NotFoundException exception) {
            return Response.status(exception.getErrorCode()).build();
        }
    }

    @Test
    public void test() throws Exception {
        Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
        dispatcher.getProviderFactory().addExceptionMapper(new FailureExceptionMapper());
        dispatcher.getRegistry().addSingletonResource(new Service());


        MockHttpRequest request = MockHttpRequest.get("/");
        MockHttpResponse response = new MockHttpResponse();

        dispatcher.invoke(request, response);

        Assert.assertEquals(404, response.getStatus());


    }
}
梨涡 2024-12-29 04:33:09

您应该在休息方法中使用:而不是抛出异常

import javax.ws.rs.core.Response;
return Response.status(404).build();

我相信,当您需要返回未找到时,

。问候。

I believe that instead of throwing an exception you should use:

import javax.ws.rs.core.Response;
return Response.status(404).build();

in your rest method when you need to return a not found.

regards.

旧情别恋 2024-12-29 04:33:09

也许自定义 javax.servlet.Filter 可以提供帮助。

Maybe a custom javax.servlet.Filter can help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文