`
elicer
  • 浏览: 131382 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Web Service handle Binary Content

阅读更多

通过JAX-WS 2.1,你能够发送binary data 在基于soap 的webservice application 中。它有两种方式来反送binary data.
1.直接发送encoded的binary data 在soap请求的body中。
2.把binary data 作为soap body的一个附件来发送
第一种发送方式的优点是比较通用,它适用于任何传输协议(SOAP,/HTTP, SOAP/JMS, and so on),但是它有一个弊端就是这些要传输的binarydata 会被JAX-WS 2.1 使用base64 encoded.这将会导致要传输的soap Message 变的很大,导致严重的性能问题。
实例代码如下:
Service Class

package itso.hello;
import javax.jws.WebService;
@WebService
public class HelloBinaryMessenger {
public byte[] sayHello(byte[] nameAsBytes) {
return String.format("Hello %s", new
String(nameAsBytes)).getBytes();
}
}


Client 端class

public class HelloBinaryClient {
public static void main(String... args) throws Exception {
HelloBinaryMessengerService service = new
HelloBinaryMessengerService();
112 IBM WebSphere Application Server V7.0 Web Services Guide
HelloBinaryMessenger port =
service.getHelloBinaryMessengerPort();
byte[] message = port.sayHello( "Milo".getBytes() );
System.out.println( new String(message) );


生成的soap request 跟soap response如下

<!-- SOAP Request Envelope -->
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:sayHello xmlns:ns2="http://hello.itso/">
<arg0>TWlsbw==</arg0>
</ns2:sayHello>
</S:Body>
</S:Envelope>

<!-- SOAP Response Envelope -->
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:sayHelloResponse xmlns:ns2="http://hello.itso/">
<return>SGVsbG8gTWlsbw==</return>
</ns2:sayHelloResponse>
</S:Body>
</S:Envelope>


如果你使用第二种方式的化,被传输的binary data 不会被encode,JAX-WS 2.1会使用MTOM(Message Transport optimize Mechanism)机制来自动处理attachment.你所要做的就是在service 端跟client 端启用MTOM.当然这种传输方式要求run times要支持SOAP 1.1/HTTP 或者SOAP 1.2/HTTP.
示例代码如下:
Server端启用MTOM,Binding Type 设成SOAPBinding.SOAP11HTTP_MTOM_BINDING:

@WebService
//Binding Type 设成SOAPBinding.SOAP11HTTP_MTOM_BINDING
@BindingType(value=SOAPBinding.SOAP11HTTP_MTOM_BINDING)
public class HelloBinaryMessenger {
public byte[] sayHello(byte[] nameAsBytes) {
return String.format("Hello %s", new
String(nameAsBytes)).getBytes();
}
}



Client端启用Enable MTOM:

SOAPBinding soapBinding = (SOAPBinding)
bindingProvider.getBinding();
soapBinding.setMTOMEnabled(true);

public class HelloBinaryClient {
public static void main(String... args) throws Exception {
HelloBinaryMessengerService service = new
HelloBinaryMessengerService();
HelloBinaryMessenger port =
service.getHelloBinaryMessengerPort();
BindingProvider bindingProvider = (BindingProvider) port;

SOAPBinding soapBinding = (SOAPBinding)
bindingProvider.getBinding();
soapBinding.setMTOMEnabled(true);
byte[] message = port.sayHello( "Milo".getBytes() );
System.out.println( new String(message) );

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics