Webservice_14_SOAP顯示的創建和處理SOAPHeader的信息

非常感謝孫浩老師。

修改服務接口方法

@WebResult(name="user")
	public List<User> getAllUser(@WebParam(header=true,name="authInfo")String authInfo);


修改實現方法

@Override
	public List<User> getAllUser(String authInfo) {
		System.out.println(authInfo);
		return users;
	}


測試類

/**
	 * @Title: test06
	 * @Description: SOAP顯示的創建和處理SOAPHeader的信息
	 * @param
	 * @return void
	 * @throws
	 */
	@Test
	public void test06() {
		try {
			// 創建訪問wsdl服務的URL
			URL url = new URL("http://localhost:9999/ns?wsdl");
			// 通過Qname指明服務的具體信息
			QName name = new QName("http://soap.lichen.cn/",
					"MyServiceImplService");
			// 創建service
			Service service = Service.create(url, name);

			// 創建dispatch
			Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(
					"http://soap.lichen.cn/", "MyServiceImplPort"),
					SOAPMessage.class, Service.Mode.MESSAGE);

			// 創建SOAPmessage
			SOAPMessage message = MessageFactory.newInstance().createMessage();
			SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
			SOAPBody body = envelope.getBody();
			
			QName qname = new QName("http://soap.lichen.cn/", "getAllUser", "xsd");
			@SuppressWarnings("unused")
			SOAPBodyElement bodyElement = body.addBodyElement(qname);
			// 輸入創建SOAPmessage
			message.writeTo(System.out);

			System.out.println("\n\n" + "-----------invoking-------------"
					+ "\n");

			// 傳遞消息並且得到結果
			SOAPMessage responseMessage = dispatch.invoke(message);

			// 輸出得到的SOAPmessage
			responseMessage.writeTo(System.out);

			// 將響應的消息轉換爲dom對象
			Document doc = responseMessage.getSOAPBody()
					.extractContentAsDocument();
			NodeList nl = doc.getElementsByTagName("user");
			JAXBContext ctx = JAXBContext.newInstance(User.class);
			for (int i = 0; i < nl.getLength(); i++) {
				Node n = nl.item(i);
				User u = (User) ctx.createUnmarshaller().unmarshal(n);
				System.out.println("\n"+u.getNickname());
			}

		} catch (SOAPException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JAXBException e) {
			e.printStackTrace();
		}
	}


 

測試端:

 

服務端:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章