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

查看:119
本文介绍了如何在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消息工厂

  1. Your WSDL correctly specifies the mime:multipartRelated on the output message
  2. We're going to use the Saaj SOAP message factory

附件驻留在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);

好的,内容:检查,留言:检查,嗯,还是需要得到回复出去。这里棘手的部分是我们需要引入 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

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天全站免登陆