JAX-RS 将实体获取为 JAXB 对象和字符串

发布于 2024-08-13 07:00:55 字数 301 浏览 3 评论 0原文

我有一个 JAX-RS Web 服务(使用球衣),它接受 JAXB 对象作为请求实体。当我们收到错误时,我们想要记录发送给我们的原始 xml 字符串。目前,我只是重新编组 JAXB 对象,但由于这些类中有几个 java 枚举,原始 xml 字符串中拼写不正确的枚举值会丢失,这对于我们的目的来说是不可接受的。

有谁知道如何以字符串和 JABX 对象的形式获取请求实体?我不想编写自定义 MessageBodyReader,并且如果可能的话,我不想尝试获取 JAXB 的 MessageBodyReader。您也可以自由使用特定于球衣的课程。我们使用的是 1.0.x 版本。

I have a JAX-RS web service (using jersey) that accepts a JAXB object as the request entity. When we get an error, we want to log the original xml string that was sent to us. Currently, I am just re-marshalling the JAXB object, but since we have several java enums in those classes, enum values that are not spelled correctly in the original xml string are lost, which is not acceptable for our purposes.

Does anyone know a way to get the request entity as both a string and JABX object? I would prefer not to write a custom MessageBodyReader and I would prefer to not try and get the MessageBodyReader for the JAXB if possible. You are free to use jersey-specific classes as well. We are using version 1.0.x.

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

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

发布评论

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

评论(2

酒几许 2024-08-20 07:00:55

事实证明,使用 JAX-RS API 来做到这一点并不难。这就是我所做的:

@Path("/transactions")
public class TestResource<X> {

    private Class<X> jaxbClass;

    @POST
    @Path("/{transaction-id}")
    @Consumes("application/xml")
    public Response processPost(@Context Providers providers, @Context HttpHeaders httpHeaders, @PathParam("transaction-id") final long transactionId,
            final String xmlString) throws WebApplicationException, IOException {

        MessageBodyReader<X> reader = providers.getMessageBodyReader(jaxbClass, null, null, MediaType.APPLICATION_XML_TYPE);
        InputStream entityStream = new ByteArrayInputStream(xmlString.getBytes());
        final X xmlObject = reader.readFrom(jaxbClass, null, null, MediaType.APPLICATION_XML_TYPE, httpHeaders.getRequestHeaders(), entityStream);

        //insert logic here

        return Response.ok().build();
    }
}

只需几行代码即可将 xml 作为字符串和 JAXB 对象提供给您。

Turns out it isn't that hard to do this with the JAX-RS API. Here's what I did:

@Path("/transactions")
public class TestResource<X> {

    private Class<X> jaxbClass;

    @POST
    @Path("/{transaction-id}")
    @Consumes("application/xml")
    public Response processPost(@Context Providers providers, @Context HttpHeaders httpHeaders, @PathParam("transaction-id") final long transactionId,
            final String xmlString) throws WebApplicationException, IOException {

        MessageBodyReader<X> reader = providers.getMessageBodyReader(jaxbClass, null, null, MediaType.APPLICATION_XML_TYPE);
        InputStream entityStream = new ByteArrayInputStream(xmlString.getBytes());
        final X xmlObject = reader.readFrom(jaxbClass, null, null, MediaType.APPLICATION_XML_TYPE, httpHeaders.getRequestHeaders(), entityStream);

        //insert logic here

        return Response.ok().build();
    }
}

This will give you the xml as a string and as a JAXB object in just a few lines of code.

尝蛊 2024-08-20 07:00:55

作为一个想法,您可以为 Web 应用程序添加一个 servlet 过滤器,该过滤器将拦截所有请求并将有效负载捕获到线程上下文中,以便稍后在需要时将其提取。

As an idea, you can add a servlet filter for your web application that would intercept all requests and capture payload into a thread context where it can be extracted from later on if needed.

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