如何在 Spring-WS 中向响应负载添加附件? [英] How do I add an attachment to a response payload in Spring-WS?

查看:28
本文介绍了如何在 Spring-WS 中向响应负载添加附件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个旨在共享内容的 Web 服务服务器.我想通过包含附件的 SOAP 响应提供内容.现在,我正在使用 Spring WS 来处理请求并提供响应.

I'm building a Web Services server that's designed to share content. I'd like to serve the content through a SOAP response containing an attachment. Right now, I'm using Spring WS to handle requests and serve responses.

我的服务类似于这样:

@Endpoint
public class Service{

    @PayloadRoot(namespace = "http://foo.com/coffee", localPart = "order")
    @ResponsePayload
    public Coffee getCoffee(@RequestPayload Order order){
        return new Coffee("Hot Joe");
    }

}

但是假设我想在回复中附上一杯咖啡的图片,我应该在哪里以及如何做?

But suppose I want to attach a picture of a cup of coffee to the response, where and how do I do that?

顺便说一句,Spring-WS 附带的示例展示了如何使用客户端发送附件,但没有展示服务器应该如何响应(这就是我在这里要问的).

edit: as an aside, the examples that ship with Spring-WS show how to use the client to send an attachment, but not how the server should respond with one (which is what I'm asking about here).

推荐答案

Spring-WS 中的文档对这个主题特别轻描淡写,实际上添加 SOAP 附件非常容易.我会做一些假设:

The documentation in Spring-WS is particularly light on this topic, and it's actually pretty easy to add a SOAP attachment. I'll make a few assumptions:

  1. 您的 WSDL 在输出消息上正确指定了 mime:multipartRelated
  2. 我们将使用 Saaj SOAP 消息工厂

附件驻留在 SOAP 消息上的 MimeContainer 中.为了获得这个容器,我们需要手动构建 SOAP 响应,好吧,只是它的一部分.这样做看起来像这样:

Attachments reside in the MimeContainer on the SOAP message. To get this container, we need to manually construct the SOAP response, well, just part of it. Doing that looks like this:

SaajSoapMessageFactory factory = new SaajSoapMessageFactory(
    javax.xml.soap.MessageFactory.newInstance());
SaajSoapMessage message = factory.createWebServiceMessage();

现在我们需要一个 DataHandler 来处理我们的图片:

Now we need a DataHandler for our picture:

DataHandler handler = new DataHandler(
    new URLDataSource(
        Service.class.getResource("coffee.jpg")));

message.addAttachment("picture", handler);

好的,content:check,message:check,嗯,还需要得到响应才能出去.这里的棘手部分是我们需要引入 MessageContext 以便我们可以将这个特定消息设置为我们响应的消息,我们通过编辑我们的定义来做到这一点getCoffee:

Okay, content: check, message: check, hm, still need to get the response to go out. The tricky part here is that we need to bring in the MessageContext so that we can set this particular message to be the one we respond with, we do that by editing our definition of getCoffee:

public Coffee getCoffee(@RequestPayload Order order, MessageContext context)

综合起来:

@Endpoint
public class Service{

    private SaajSoapMessageFactory saajMessageFactory; // dependency inject this

    @PayloadRoot(namespace = "http://foo.com/coffee", localPart = "order")
    @ResponsePayload
    public Coffee getCoffee(@RequestPayload Order order, MessageContext context){
        DataHandler handler = new DataHandler(
            new URLDataSource(
                Service.class.getResource("coffee.jpg")));

        SaajSoapMessage message = saajMessageFactory.createWebServiceMessage();
        message.addAttachment("picture", handler);

        context.setResponse(message);

        return new Coffee("Hot Joe");
    }

    public void setSaajMessageFactory(SaajMessageFactory saajMessageFactory){
        this.saajMessageFactory = saajMessageFactory;
    }

    public SaajMessageFactory getSaajMessageFactory(){
        return saajMessageFactory;
    }

}

为了更好的衡量,这里是获取 SaajMessageFactory 的 bean 依赖注入:

For good measure, here's the beans dependency injection for getting a SaajMessageFactory:

<bean id="soapMessageFactory" class="javax.xml.soap.MessageFactory" factory-method="newInstance" />
<bean id="saajMessageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
    <constructor-arg ref="soapMessageFactory" />
</bean>

<bean id="myService" class="Service">
    <property name="saajMessageFactory" ref="saajMessageFactory" />
</bean>

这篇关于如何在 Spring-WS 中向响应负载添加附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆