Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6068839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:45:29+00:00 2026-05-23T09:45:29+00:00

I’m building a Web Services server that’s designed to share content. I’d like to

  • 0

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.

My service resembles something like this:

@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?

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:45:29+00:00Added an answer on May 23, 2026 at 9:45 am

    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. Your WSDL correctly specifies the mime:multipartRelated on the output message
    2. We’re going to use the Saaj SOAP message factory

    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();
    

    Now we need a DataHandler for our picture:

    DataHandler handler = new DataHandler(
        new URLDataSource(
            Service.class.getResource("coffee.jpg")));
    
    message.addAttachment("picture", handler);
    

    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)
    

    Putting it all together:

    @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;
        }
    
    }
    

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.