2

I have Soap WS on Java.
Here is soap request

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:his="SCC/Lis/HistoryFormatter">
   <soapenv:Header/>
   <soapenv:Body>
      <his:formatHistoryByteArray>
         <arg0>cid:anystring</arg0>
      </his:formatHistoryByteArray>
   </soapenv:Body>
</soapenv:Envelope>  

FormatHistoryByteArray.class has only one field

@XmlElement(name = "arg0", namespace = "", nillable = true)
private byte[] arg0;  

Type in *.xsd

  <xs:complexType name="formatHistoryByteArray">
    <xs:sequence>
      <xs:element name="arg0" type="xs:base64Binary" nillable="true" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

WSDL and xsd is generated by JaxWS.
I cann't understand logic of convertion string in node in request to byte[] in java-code. Help plz
cid: is the requared prefix or not?

Edited: for example, if I have request

<arg0>abcdef</arg0>  

in java code I get byte[] = {105, -73, 29}

How WebService get this byte array from string abcdef?

Ilya
  • 29,135
  • 19
  • 110
  • 158
  • Your question is still not clear. If you're looking to convert string to byte[] then use `stringValue.getBytes()` – Bitmap Apr 23 '12 at 13:25
  • WebService automaticaly convert String to byte[], but this result is different with stringValue.getBytes() result. – Ilya Apr 23 '12 at 13:34
  • 1
    No it doesn't. Your complexType defined for `formatHistoryByteArray` is a base64 encoded value, you'll have use either `org.apache.commons.codec.binary.Base64` or similar to decode the value back to byte[] or object. – Bitmap Apr 23 '12 at 13:42
  • @Bitmap Doesn't JAXB do that conversion automatically? – artbristol Apr 23 '12 at 13:43
  • `xs:base64Binary` has been specified explicitly as your value type, therefore binding object will hold encoded bytes. – Bitmap Apr 23 '12 at 13:48

2 Answers2

4

String.getBytes() returns you the (ASCII, UTF8, ISO-8859-1 etc.) encoding of a given String. That's different from what Base 64 is. Base 64 is a way of displaying arbitrary bytes as printable characters. So there is no reason for them to be the same.

Have a look at section 2.1 of this tutorial on Base 64 and XML: http://www.xml.com/pub/a/2003/02/26/binaryxml.html. The base64 bit looks like this:

<m:data xmlns:m='http://example.org/people' >
  <photo>/aWKKapGGyQ=</photo>
  <sound>sdcfo2JTiXE=</sound>
  <hash>Faa7vROi2VQ=</hash>
</m:data>

where photo etc. are base64 elements. cid prefix is not needed.

To address your question, abcdef is being interpreted by the web service unmarshaller as a base-64 encoded string as the three bytes you received.

artbristol
  • 32,010
  • 5
  • 70
  • 103
0

The schema specifically declares the type as: "xs:base64Binary" aka: BINARY. If you expect the the information to be textual in nature, the type should likely be "xs:string" or similar.

Actually, the service should be rejecting the value of "cid:anystring" entirely. That's not a valid value for a base64 encoded element.

Daniel Kulp
  • 14,447
  • 4
  • 45
  • 37