How to change encoding from UTF-8 to UTF-16

Support for Universal Transformation Format (UTF)-16 encoding is required by the WS-I Basic Profile 1.0. WAS conforms to the WS-I Basic Profile 1.1. UTF-16 is a kind of unicode encoding scheme using 16-bit values to store Universal Character Set (UCS) characters. UTF-8 is the most common encoding that is used on the Internet and UTF-16 encoding is typically used for Java and Windows product applications.

 

Before you begin

To learn more about the requirements of the Web Services-Interoperability Basic Profile (WS-I), including UTF-16, see Web Services-Interoperability Basic Profile.

 

Overview

Support for UTF-16 encoding is required by WS-I Basic Profile; therefore, we need to know how to modify your character encoding from UTF-8 to UTF-16 in your SOAP message. We can change the character encoding in one of two ways:

 

Steps for this task (dependent on configuration)

  • Use a property on the Stub for users to set.

    This choice applies to the client only.

    For a client, the encoding is specified in the SOAP request. The SOAP engine serializes the request and sends it to the Web service engine. The Web service engine receives the request and deserializes the message to Java objects, which are returned to you in a response.

    When the Web service engine on the server receives the serialized request, a raw message in the form of an input stream, is passed to the parser, which understands Byte Order Mark (BOM). BOM is mandatory for UTF-16 encoding and it can be used in UTF-8. The message is deserialized to a Java objects and a service invocation is made. For two-way invocation, the engine needs to serialize the message using a specific encoding and send it back to the caller. The following example shows you how to use a property on the Stub to change the character set

    javax.xml.rpc.Stub stub=service.getPort("MyPortType");
    stub.setProperty(com.ibm.wsspi.webservices.Constants.XML_CHARSET,"UTF-16");
    stub.invokeMethod();
    
    
    
    In this code example, com.ibm.wsspi.webservices.Constants.XML_CHARSET = "com.ibm.wsspi.webservices.xmlcharset";

  • Use a handler to change the character set through SOAP with Attachments API for Java (SAAJ).

    If you are using a handler, the SOAP message is transformed to a SAAJ format from other possible forms, such as an input stream. In such cases as a handleRequest method on the client side and a handleResponse method on the server side, the Web services engine transforms from a SAAJ format back to the stream with appropriate character encoding. This transformation or change is called a roundtrip transformation. The following is an example of how you would use a handler to specify the character encoding through SAAJ

    handleResponse(MessageContext mc) {
      SOAPMessageContext smc = (SOAPMessageContext) context;
      javax.xml.soap.SOAPMessage msg = smc.getMessage();
      msg.setProperty (javax.xml.soap.SOAPMessage.CHARACTER_SET_ENCODING, "UTF-16");
      }
    }
    

 

Result

You have modified the character encoding from UTF-8 to UTF-16 in the Web service SOAP message.


 

See Also


Web Services-Interoperability Basic Profile