JAVA6开发WebService (二)——JAX-WS例子

上一篇写了个最简单的小例子,只是为了说明JAVA6开发Web Service很方便,这一篇稍微深入一点,写个稍微有点代表性的小例子。

依然使用 JAX-WS(jdk自带的实现)方式,这次要在服务中使用一个复杂类型Customer,并实现附件传输的功能,这里使用MTOM的附件传输方式。MTOM(SOAP Message Transmission Optimization Mechanism)是SOAP 消息传输优化机制,MTOM可以在SOAP 消息中发送二进制数据。

先来看Customer类:

  1. package com.why.server;
  2. import java.util.Date;
  3. import javax.activation.DataHandler;
  4. import javax.xml.bind.annotation.XmlAccessType;
  5. import javax.xml.bind.annotation.XmlAccessorType;
  6. import javax.xml.bind.annotation.XmlMimeType;
  7. import javax.xml.bind.annotation.XmlRootElement;
  8. @XmlRootElement(name ="Customer")
  9. @XmlAccessorType(XmlAccessType.FIELD)
  10. public class Customer {
  11. private long id;
  12. private String name;
  13. private Date birthday;
  14. @XmlMimeType("application/octet-stream")
  15. private DataHandler imageData;
  16. //getter and setter
  17. ......
  18. }

MTOM 方式中要传输的附件必须使用javax.activation.DataHandler 类,还要注意必须在类上使用@XmlAccessorType(FIELD)注解,标注JAXB 在进行JAVA 对象与XML 之间进行转换时只关注字段,而不关注属性(getXXX()方法),否则发布Web 服务时会报出现了两个imageData 属性的错误,原因未知,可能是BUG。

然后使用@XmlMimeType 注解标注这是一个附件类型的数据,这里我们标注imageData 是一个二进制文件,当然你也可以使用具体的MIME类型,譬如:image/jpg、image/gif 等,但要考虑到客户端是否支持。

接口类:

  1. package com.why.server;
  2. import javax.jws.WebParam;
  3. import javax.jws.WebService;
  4. import javax.xml.ws.soap.MTOM;
  5. /**
  6. *
  7. * @author why
  8. *
  9. */
  10. @WebService(name="Hello")
  11. @SOAPBinding(style = SOAPBinding.Style.RPC)
  12. @MTOM
  13. public interface Hello {
  14. public void printContext();
  15. public Customer selectCustomerByName(@WebParam(name ="customer")Customer customer);
  16. public Customer selectMaxAgeCustomer(Customer c1, Customer c2);
  17. }
  18.          

@MTOM注解用于开启MTOM功能。

@WebService注解中的name属性标注在接口类上,可以指定wsdl中接口名称,也就是生成的客户端代码中接口类的名字。

@SOAPBinding(style = SOAPBinding.Style.RPC)指定SOAP消息样式,有两个枚举值:SOAPBinding.Style.DOCUMENT(默认)和SOAPBinding.Style.RPC,可以对比这两种方式生成的wsdl会有所不同,而且生成的客户端代码也会有所不同。

实现类:

  1. package com.why.server;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.text.ParseException;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Date;
  10. import java.util.Set;
  11. import javax.activation.DataHandler;
  12. import javax.activation.FileDataSource;
  13. import javax.annotation.Resource;
  14. import javax.jws.WebService;
  15. import javax.xml.ws.WebServiceContext;
  16. import javax.xml.ws.handler.MessageContext;
  17. /**
  18. *
  19. * @author why
  20. *
  21. */
  22. @WebService(serviceName="HelloService",portName="HelloServicePort",targetNamespace="http://service.why.com/",endpointInterface="com.why.server.Hello")
  23. public class HelloImplimplements Hello {
  24. @Resource
  25. private WebServiceContext context;
  26. @Override
  27. public void printContext(){
  28. MessageContext ctx = context.getMessageContext();
  29. Set<String> set = ctx.keySet();
  30. for (String key : set) {
  31. System.out.println("{" + key +"," + ctx.get(key) +"}");
  32. try {
  33. System.out.println("key.scope=" + ctx.getScope(key));
  34. } catch (Exception e) {
  35. System.out.println(key + " is not exits");
  36. }
  37. }
  38. }
  39. @Override
  40. public Customer selectCustomerByName(Customer customer) {
  41. if("why".equals(customer.getName())){
  42. customer.setId(1);
  43. try {
  44. customer.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("1985-10-07"));
  45. } catch (ParseException e) {
  46. e.printStackTrace();
  47. }
  48. customer.setImageData(new DataHandler(new FileDataSource(new File("c:"+ File.separator + "why.jpg"))));
  49. }else{
  50. customer.setId(2);
  51. customer.setBirthday(new Date());
  52. customer.setImageData(new DataHandler(new FileDataSource(new File("c:"+ File.separator + "origin.jpg"))));
  53. }
  54. return customer;
  55. }
  56. @Override
  57. public Customer selectMaxAgeCustomer(Customer c1, Customer c2) {
  58. try {
  59. // 输出接收到的附件
  60. System.out.println("c1.getImageData().getContentType()=" + c1.getImageData().getContentType());
  61. InputStream is = c2.getImageData().getInputStream();
  62. OutputStream os = new FileOutputStream("c:\\temp1.jpg");
  63. byte[] bytes = new byte[1024];
  64. int c;
  65. while ((c = is.read(bytes)) != -1) {
  66. os.write(bytes, 0, c);
  67. }
  68. os.close();
  69. System.out.println("c2.getImageData().getContentType()=" + c2.getImageData().getContentType());
  70. is = c2.getImageData().getInputStream();
  71. os = new FileOutputStream("c:\\temp2.jpg");
  72. bytes = new byte[1024];
  73. while ((c = is.read(bytes)) != -1) {
  74. os.write(bytes, 0, c);
  75. }
  76. os.close();
  77. } catch (IOException e) {
  78. e.printStackTrace();
  79. }
  80. if (c1.getBirthday().getTime() > c2.getBirthday().getTime()){
  81. return c2;
  82. }
  83. else{
  84. return c1;
  85. }
  86. }
  87. }

@WebService注解的serviceName属性指定wsdl中service节点的name属性值。portName属性指定wsdl中service节点下port节点name属性值。targetNamespace属性指定wsdl根节点definitions的targetNamespace属性值。endpointInterface属性指定要发布的WebService接口的全路径名,当实现类实现了多个接口时,需要通过此属性标注哪个类是WebService的服务端点接口(SEI)。

在这个类中,通过@Resource注解注入了一个WebServiceContext对象,这个对象即是WebService的上下文环境。

发布这个服务:

  1. package com.why.server;
  2. import javax.xml.ws.Endpoint;
  3. /**
  4. *
  5. * @author why
  6. *
  7. */
  8. public class SoapServer {
  9. public staticvoid main(String[] args) {
  10. Endpoint.publish("http://localhost:8080/helloService",new HelloImpl());
  11. }
  12. }

在命令行键入“wsimport -p com.why.client -keep http://localhost:8080/helloService?wsdl”生成客户端代码,拷贝到工程相应文件夹里,这时,就可以调用这个服务了:

  1. package com.why.client;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.text.ParseException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.GregorianCalendar;
  11. import javax.activation.DataHandler;
  12. import javax.activation.DataSource;
  13. import javax.activation.FileDataSource;
  14. import javax.xml.datatype.DatatypeConfigurationException;
  15. import javax.xml.datatype.DatatypeFactory;
  16. import javax.xml.namespace.QName;
  17. /**
  18. *
  19. * @author why
  20. *
  21. */
  22. public class SoapClient {
  23. public staticvoid main(String[] args) throws ParseException, MalformedURLException {
  24. QName qName = new QName("http://service.why.com/","HelloService");
  25. HelloService helloService = new HelloService(new URL("http://127.0.0.1:8080/helloService?wsdl"),qName);
  26. Hello hello = (Hello) helloService.getPort(Hello.class);
  27. hello.printContext();
  28. System.out.println("---------------------------------------------------");
  29. Customer customer = new Customer();
  30. customer.setName("why");
  31. DataSource ds = hello.selectCustomerByName(customer).getImageData().getDataSource();
  32. String attachmentMimeType = ds.getContentType();
  33. System.out.println(attachmentMimeType);
  34. try {
  35. InputStream is = ds.getInputStream();
  36. OutputStream os = new FileOutputStream("c:\\why_temp.jpg");
  37. byte[] bytes = new byte[1024];
  38. int c;
  39. while ((c = is.read(bytes)) != -1) {
  40. os.write(bytes, 0, c);
  41. }
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. }
  45. System.out.println("########################################");
  46. Customer c1 = new Customer();
  47. c1.setId(1);
  48. c1.setName("why");
  49. GregorianCalendar calendar = (GregorianCalendar)GregorianCalendar.getInstance();
  50. calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse("1985-10-07"));
  51. try {
  52. c1.setBirthday(DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar));
  53. } catch (DatatypeConfigurationException e) {
  54. e.printStackTrace();
  55. }
  56. c1.setImageData(new DataHandler(new FileDataSource("c:\\c1.jpg")));
  57. Customer c2 = new Customer();
  58. c2.setId(2);
  59. c2.setName("abc");
  60. calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse("1986-10-07"));
  61. try {
  62. c2.setBirthday(DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar));
  63. } catch (DatatypeConfigurationException e) {
  64. e.printStackTrace();
  65. }
  66. c2.setImageData(new DataHandler(new FileDataSource("c:\\c2.jpg")));
  67. Customer c = hello.selectMaxAgeCustomer(c1,c2);
  68. System.out.println(c.getName());
  69. }
  70. }

附件是我的工程,当然运行这个程序,需先在C盘建立几个文件c1.jpg、c2.jpg、origin.jpg和why.jpg。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值