Spring 集成解组

发布于 2025-01-15 03:25:09 字数 1570 浏览 5 评论 0原文

我正在尝试使用 UnmarshallingTransformer 在 Spring 集成中将 java 对象解组为 xml,但收到错误消息,表明未创建有效负载。 这是我的代码:


@Bean
    public Marshaller jaxbMarshaller() {
        Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
        jaxb2Marshaller.setClassesToBeBound(TargetedClass.class);

        return jaxb2Marshaller;
    }

@Bean
    public IntegrationFlow posting() {


        try {

            return IntegrationFlows.from(Http.inboundGateway("/foo")
                            .requestMapping(m -> m.methods(HttpMethod.POST)
                                    
                            ).errorChannel("error.input")
                    )

                    .transform(Transformers.objectToString())

                    .enrichHeaders(h -> h.headerExpression())
                    .transform(httpcallFunc())
                    .transform(new UnmarshallingTransformer((Unmarshaller) jaxbMarshaller()))

                    .get();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

我收到此错误。

org.springframework.messaging.MessagingException: failed to create Source for payload type [com.org.model.ClassName]
org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 : "<Map><timestamp>2022-03-18T11:24:26.568+00:00</timestamp><status>500</status><error>Internal Server Error</error><path>/foo</path></Map>"

当我尝试使用 UnmarshallingTransformer 时会发生这种情况。

I am trying to unmarshal java objects to xml in Spring integration using UnmarshallingTransformer but I am getting an error that the payload is not created.
This is my code :


@Bean
    public Marshaller jaxbMarshaller() {
        Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
        jaxb2Marshaller.setClassesToBeBound(TargetedClass.class);

        return jaxb2Marshaller;
    }

@Bean
    public IntegrationFlow posting() {


        try {

            return IntegrationFlows.from(Http.inboundGateway("/foo")
                            .requestMapping(m -> m.methods(HttpMethod.POST)
                                    
                            ).errorChannel("error.input")
                    )

                    .transform(Transformers.objectToString())

                    .enrichHeaders(h -> h.headerExpression())
                    .transform(httpcallFunc())
                    .transform(new UnmarshallingTransformer((Unmarshaller) jaxbMarshaller()))

                    .get();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

I am getting this error back.

org.springframework.messaging.MessagingException: failed to create Source for payload type [com.org.model.ClassName]
org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 : "<Map><timestamp>2022-03-18T11:24:26.568+00:00</timestamp><status>500</status><error>Internal Server Error</error><path>/foo</path></Map>"

This occurs when I am trying to use the UnmarshallingTransformer.

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

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

发布评论

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

评论(1

娇俏 2025-01-22 03:25:09

UnmarshallingTransformer 默认情况下使用 DomSourceFactory。它的逻辑是这样的:

        if (this.alwaysUseSourceFactory) {
            source = this.sourceFactory.createSource(payload);
        }
        else if (payload instanceof String) {
            source = new StringSource((String) payload);
        }
        else if (payload instanceof byte[]) {
            source = new StreamSource(new ByteArrayInputStream((byte[]) payload));
        }
        else if (payload instanceof File) {
            File file = (File) payload;
            inputStream = new FileInputStream(file);
            source = new StreamSource(inputStream, file.toURI().toASCIIString());
        }
        else if (payload instanceof Document) {
            source = new DOMSource((Document) payload);
        }
        else if (payload instanceof Source) {
            source = (Source) payload;
        }
        else {
            source = this.sourceFactory.createSource(payload);
        }

由于默认选项和您的自定义类型com.org.model.ClassName,没有任何条件适用,因此它无法执行最后一个elseDomSourceFactory 的逻辑如下:

public Source createSource(Object payload) {
    Source source = null;
    if (payload instanceof Document) {
        source = createDomSourceForDocument((Document) payload);
    }
    else if (payload instanceof String) {
        source = createDomSourceForString((String) payload);
    }
    else if (payload instanceof File) {
        source = createDomSourceForFile((File) payload);
    }
    if (source == null) {
        throw new MessagingException("failed to create Source for payload type [" +
                payload.getClass().getName() + "]");
    }
    return source;
}

这也不满足您的 com.org.model.ClassName。因此,您会在日志中看到异常等等。

我不确定您的期望是什么,但是您从 httpcallFunc() 生成的类型与 UnmarshallingTransformer 不兼容。

The UnmarshallingTransformer uses a DomSourceFactory by default. And its logic is like this:

        if (this.alwaysUseSourceFactory) {
            source = this.sourceFactory.createSource(payload);
        }
        else if (payload instanceof String) {
            source = new StringSource((String) payload);
        }
        else if (payload instanceof byte[]) {
            source = new StreamSource(new ByteArrayInputStream((byte[]) payload));
        }
        else if (payload instanceof File) {
            File file = (File) payload;
            inputStream = new FileInputStream(file);
            source = new StreamSource(inputStream, file.toURI().toASCIIString());
        }
        else if (payload instanceof Document) {
            source = new DOMSource((Document) payload);
        }
        else if (payload instanceof Source) {
            source = (Source) payload;
        }
        else {
            source = this.sourceFactory.createSource(payload);
        }

Since no one condition applies because of the default options and your custom type com.org.model.ClassName, it fails to the last else. The DomSourceFactory has a logic like this:

public Source createSource(Object payload) {
    Source source = null;
    if (payload instanceof Document) {
        source = createDomSourceForDocument((Document) payload);
    }
    else if (payload instanceof String) {
        source = createDomSourceForString((String) payload);
    }
    else if (payload instanceof File) {
        source = createDomSourceForFile((File) payload);
    }
    if (source == null) {
        throw new MessagingException("failed to create Source for payload type [" +
                payload.getClass().getName() + "]");
    }
    return source;
}

Which also does not satisfy your com.org.model.ClassName. Therefore an exception you see in your logs and so.

I'm not sure what is your expectations, but the type you produce from your httpcallFunc() is not what would be compatible with the UnmarshallingTransformer.

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