Announcement Announcement Module
Collapse
No announcement yet.
Specifying encoding for web service client Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Specifying encoding for web service client

    Hello,

    I am new to WS. Basically, instead of UTF-8 I need to send a message with <?xml version="1.0" encoding="ISO-8859-1"?>

    I am building my xml message using groovy.xml.MarkupBuilder and using WebServiceTemplate to send.

    For the outgoing SOAP XML, can I specify the encoding for the outgoing message when I develop client application with Spring Web-service?

    Thanks in advance..

    Ekrem

    Here is my code:

    class RequestSender {

    @Autowired
    WebServiceTemplate webServiceTemplate

    public def send(def request, String url) {

    String decodedResponse

    //request is xml message that is build by groovy.xml.MarkupBuilder

    try {
    String requestValue = request.bytes.encodeBase64().toString()

    def writer = new StringWriter()
    def xml = new MarkupBuilder(writer)
    xml.Message() {
    Request(xmlns: "", requestValue)
    }

    SoapActionCallback soapActionCallback = new SoapActionCallback("${url}/Message") {
    public void doWithMessage(WebServiceMessage msg) {
    SoapMessage smsg = (SoapMessage)msg
    smsg.setSoapAction("${url}/Message")
    }
    }

    DOMResult result = new DOMResult()
    StreamSource source = new StreamSource(new StringReader(writer.toString()))

    webServiceTemplate.sendSourceAndReceiveToResult("$ {url}", source, soapActionCallback, result)

    Element responseElement = (Element) result.getNode().getFirstChild()
    String response = responseElement.firstChild.firstChild.data
    decodedResponse = new String(response.decodeBase64())

    } catch (Exception e) {}

    return decodedResponse
    }
    }

    Libs used:
    spring-ws-core:2.1.3.RELEASE
    spring-ws:2.1.3.RELEASE
    Last edited by ekamberoglu; Jun 1st, 2013, 05:56 AM.

  • #2
    Since you are using this:
    Code:
    StreamSource source = new StreamSource(new StringReader(writer.toString()))
    Looks like you can simple do this:
    Code:
    String xml = writer.toString().replaceFirst(/(encoding=")UTF-8("\?>)/, '$1ISO-8859-1$2')
    StreamSource source = new StreamSource(new StringReader(xml))

    Comment

    Working...
    X