gpt4 book ai didi

java - 传递不正确的媒体类型时无法捕获 WebApplicationException

转载 作者:行者123 更新时间:2023-12-01 14:50:15 27 4
gpt4 key购买 nike

我想捕获所有类型的 Throwable以便在发生异常时客户端不会暴露于我们的内部堆栈跟踪。

我正在使用ExceptionMapper<Throwable>如下:

@Provider
public class MappableContainerExceptionMapper implements ExceptionMapper<Throwable>
{

@Override
public Response toResponse(Throwable exception)
{
return Response.status(Response.Status.BAD_REQUEST).entity("caught").build();
}

}

但是,当经过Content-Type=asdfasdf时,我得到向客户端显示的以下堆栈跟踪:

javax.ws.rs.WebApplicationException: java.lang.IllegalArgumentException: Error parsing media type 'sdfsdfdvfv'
com.sun.jersey.server.impl.model.HttpHelper.clientError(HttpHelper.java:265)
com.sun.jersey.server.impl.model.HttpHelper.getContentType(HttpHelper.java:91)
com.sun.jersey.server.impl.model.HttpHelper.getContentType(HttpHelper.java:77)
com.sun.jersey.spi.container.ContainerRequest.getMediaType(ContainerRequest.java:651)
com.sun.jersey.spi.container.servlet.WebComponent.filterFormParameters(WebComponent.java:832)
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:407)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
</pre></p><p><b>root cause</b> <pre>java.lang.IllegalArgumentException: Error parsing media type 'sdfsdfdvfv'
com.sun.jersey.core.impl.provider.header.MediaTypeProvider.fromString(MediaTypeProvider.java:79)
com.sun.jersey.core.impl.provider.header.MediaTypeProvider.fromString(MediaTypeProvider.java:53)
javax.ws.rs.core.MediaType.valueOf(MediaType.java:119)
com.sun.jersey.server.impl.model.HttpHelper.getContentType(HttpHelper.java:89)
com.sun.jersey.server.impl.model.HttpHelper.getContentType(HttpHelper.java:77)
com.sun.jersey.spi.container.ContainerRequest.getMediaType(ContainerRequest.java:651)

我也想捕获这个,以便客户端显示漂亮的 JSON。

最佳答案

这看起来像是一个边界实现错误 - 但无论如何都可以争论。 specification 第 3.7.2 节关于不支持的输入媒体类型的处理有这样的说法:

The media type of the request entity body (if any) is a supported input data format (see section 3.5). If no methods support the media type of the request entity body an implementation MUST generate a WebApplicationException with an unsupported media type response (HTTP 415 status) and no entity. The exception MUST be processed as described 4 in section 3.3.4.

所以问题是,不可解析的内容是否属于“不支持的输入数据格式”的范围。我猜 Jersey 实现团队决定不这样做。因此,他们抛出一个 WebApplicationException 带有实体主体,该实体主体紧邻您的异常提供程序:

Instances of WebApplicationException MUST be mapped to a response as follows. If the response property of the exception does not contain an entity and an exception mapping provider (see section 4.4) is available for WebApplicationException an implementation MUST use the provider to create a new Response instance, otherwise the response property is used directly.

你无法解决这个问题,因为代码位于 Jersey 库本身中 - 你最希望做的就是在 bug report 中争论你的情况。 .

关于java - 传递不正确的媒体类型时无法捕获 WebApplicationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14958579/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com