通过JAXBContext创建Marshaller对头信息进行解析为dom,获取WSBindingProvider,使用Headers.creat()创建soap的Header元素;
另外就是:将user转换为dom是为了不在有wsdl生成的LicenceInfo类中增加@WebRootElement
1 public void testAdd_1(){ 2 try { 3 JAXBContext jaxbc = JAXBContext.newInstance(LicenceInfo.class); 4 User u = new User(); 5 u.setId(111); 6 u.setUsername("yyyy"); 7 u.setPassword("oooooo"); 8 LicenceInfo info = new LicenceInfo(); 9 info.setRegisterUser(u); 10 //转化为element进行解析 11 QName qname = new QName("http://www.xiaoqiaolv/ws","licenceInfo"); 12 JAXBElementele = new JAXBElement (qname, LicenceInfo.class, info); 13 14 Marshaller marsh = jaxbc.createMarshaller(); 15 marsh.setProperty(Marshaller.JAXB_FRAGMENT, true); 16 marsh.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); 17 // marsh.marshal(ele, System.out); 18 //转化为dom 19 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 20 marsh.marshal(ele, doc); 21 // userServiceImpl = new UserServiceImpl(); 22 // userService = userServiceImpl.getUserServiceImplPort(); 23 //用下面的四行代替上面的两行内容是为了用tcpmon来监听两个接口之间的通信 24 URL url = new URL("http://localhost:8888/WebService_Server/ws?wsdl"); 25 QName qname1 = new QName("http://www.xiaoqiaolv/ws","UserServiceImpl"); 26 userServiceImpl = new UserServiceImpl(url,qname1); 27 userService = userServiceImpl.getUserServiceImplPort(); 28 //通过Headers.creat();完成header的添加 29 //获取WSBindingProvider 30 WSBindingProvider wsb = (WSBindingProvider)userService; 31 wsb.setOutboundHeaders(Headers.create(doc.getDocumentElement())); 32 33 User user = new User(); 34 user.setUsername("aaa"); 35 user.setPassword("1111"); 36 user.setId(21212); 37 userService.add(user); 38 } catch (Exception e) { 39 e.printStackTrace(); 40 } 41 }