通过JAXBContext创建Marshaller对头信息进行解析为dom,获取WSBindingProvider,使用Headers.creat()创建soap的Header元素;
另外就是:将user转换为dom是为了不在有wsdl生成的LicenceInfo类中增加@WebRootElement
public void testAdd_1(){
try {
JAXBContext jaxbc = JAXBContext.newInstance(LicenceInfo.class);
User u = new User();
u.setId(111);
u.setUsername("yyyy");
u.setPassword("oooooo");
LicenceInfo info = new LicenceInfo();
info.setRegisterUser(u);
//转化为element进行解析
QName qname = new QName("http://www.xiaoqiaolv/ws","licenceInfo");
JAXBElement<LicenceInfo> ele = new JAXBElement<LicenceInfo>(qname, LicenceInfo.class, info); Marshaller marsh = jaxbc.createMarshaller();
marsh.setProperty(Marshaller.JAXB_FRAGMENT, true);
marsh.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
// marsh.marshal(ele, System.out);
//转化为dom
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
marsh.marshal(ele, doc);
// userServiceImpl = new UserServiceImpl();
// userService = userServiceImpl.getUserServiceImplPort();
//用下面的四行代替上面的两行内容是为了用tcpmon来监听两个接口之间的通信
URL url = new URL("http://localhost:8888/WebService_Server/ws?wsdl");
QName qname1 = new QName("http://www.xiaoqiaolv/ws","UserServiceImpl");
userServiceImpl = new UserServiceImpl(url,qname1);
userService = userServiceImpl.getUserServiceImplPort();
//通过Headers.creat();完成header的添加
//获取WSBindingProvider
WSBindingProvider wsb = (WSBindingProvider)userService;
wsb.setOutboundHeaders(Headers.create(doc.getDocumentElement())); User user = new User();
user.setUsername("aaa");
user.setPassword("1111");
user.setId(21212);
userService.add(user);
} catch (Exception e) {
e.printStackTrace();
}
}