JAX-WS创建和JavaAPI发送和处理SOAP消息实例
来源:互联网 发布:5g网络wifi什么意思 编辑:程序博客网 时间:2024/04/27 10:25
这篇文章就对处理web services的JAVA基础API JAX-WS进行介绍。
首先看如何发布web services服务。
1、编写接口,使用@WebService注解
package zmx.jaxws.server;import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; @WebService public interface IMyService { @WebResult(name="addResult") //对返回值和参数进行名字定义,否则默认为arg0,arg1... public int add(@WebParam(name="firstA")int a ,@WebParam(name="SecondB")int b); //与上面方法进行对比 public int minus(int a,int b); }
2、编写编写实现类,标明@WebService注解
package zmx.jaxws.server;import javax.jws.WebResult;import javax.jws.WebService;@WebService(endpointInterface="zmx.jaxws.server.IMyService")public class MyServiceImpl implements IMyService {@Override@WebResult(name="addResult")public int add(int a, int b) {System.out.print(a+"+"+b+"="+(a+b));return a+b;}@Overridepublic int minus(int a, int b) {System.out.print(a+"-"+b+"="+(a-b));return a-b;}}
3、编写服务类,根据url和是实现类启动服务
package zmx.jaxws.server;import javax.xml.ws.Endpoint;public class MyServicePublisher {public static void main(String[] args) {String address="http://localhost:6666/ws/myService"; Endpoint.publish(address, new MyServiceImpl()); System.out.println("WS Published."); }}
4、运行3中代码,发布服务,在浏览器可查看
http://localhost:6666/ws/myService?wsdl
<?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. -->-<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" name="MyServiceImplService" targetNamespace="http://server.jaxws.zmx/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://server.jaxws.zmx/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">-<types>-<xsd:schema><xsd:import schemaLocation="http://localhost:6666/ws/myService?xsd=1" namespace="http://server.jaxws.zmx/"/></xsd:schema></types>-<message name="add"><part name="parameters" element="tns:add"/></message>-<message name="addResponse"><part name="parameters" element="tns:addResponse"/></message>-<message name="minus"><part name="parameters" element="tns:minus"/></message>-<message name="minusResponse"><part name="parameters" element="tns:minusResponse"/></message>-<portType name="IMyService">-<operation name="add"><input message="tns:add" wsam:Action="http://server.jaxws.zmx/IMyService/addRequest"/><output message="tns:addResponse" wsam:Action="http://server.jaxws.zmx/IMyService/addResponse"/></operation>-<operation name="minus"><input message="tns:minus" wsam:Action="http://server.jaxws.zmx/IMyService/minusRequest"/><output message="tns:minusResponse" wsam:Action="http://server.jaxws.zmx/IMyService/minusResponse"/></operation></portType>-<binding type="tns:IMyService" name="MyServiceImplPortBinding"><soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>-<operation name="add"><soap:operation soapAction=""/>-<input><soap:body use="literal"/></input>-<output><soap:body use="literal"/></output></operation>-<operation name="minus"><soap:operation soapAction=""/>-<input><soap:body use="literal"/></input>-<output><soap:body use="literal"/></output></operation></binding>-<service name="MyServiceImplService">-<port name="MyServiceImplPort" binding="tns:MyServiceImplPortBinding"><soap:address location="http://localhost:6666/ws/myService"/></port></service></definitions>
5、使用jax-ws封装好的服务进行客户端调用
package zmx.jaxws.client;import java.net.URL;import javax.xml.namespace.QName;import javax.xml.ws.Service;import zmx.jaxws.server.IMyService;public class MyServiceClient {public static void main(String[] args) throws Exception { URL url = new URL("http://localhost:6666/ws/myService?wsdl"); QName qname = new QName("http://server.jaxws.zmx/", "MyServiceImplService"); Service service = Service.create(url, qname); IMyService myService = service.getPort(IMyService.class); System.out.println(myService.add(12,33)); }}
这样的客户端调用直接使用封装好的service进行操作,在调用过程中将我们的代码转换为SOAP“信件”与服务进行交互,我们不需要编写任何生成和处理SOAP消息的代码。很是方便。不过既然是JAVA 底层API,它也提供了底层的SOAP操作API,我们来用易用,了解一下SOAP是怎样生成的。
6、创建SOAP消息
private String ns = "http://server.jaxws.zmx/";private String wsdlUrl = "http://localhost:6666/ws/myService?wsdl";/** * 创建SOAP消息 * @author zmx */@Test public void createSoap() {try {// 创建消息工厂MessageFactory factory = MessageFactory.newInstance();// 根据消息工厂创建SoapMessageSOAPMessage message = factory.createMessage();// 创建SOAPPartSOAPPart part = message.getSOAPPart();// 获取SOAPEnvelopeSOAPEnvelope envelope = part.getEnvelope(); // 通过SoapEnvelope可以获取到相应的Body和Header等信息SOAPBody body = envelope.getBody();// 根据Qname创建相应的节点,Qname是一个带有命名空间的节点QName qname = new QName("http://server.jaxws.zmx/", "add", "ns");// <ns:add xmlns="http://java.wh/webservice/"SOAPBodyElement ele = body.addBodyElement(qname);ele.addChildElement("a").setValue("11");ele.addChildElement("b").setValue("22");// 打印消息信息message.writeTo(System.out);} catch (Exception e) {e.printStackTrace();}}
代码比较容易理解,首先通过消息工厂MessageFactory创建SOAPMessage,然后创建SOAPPart,我的理解相当于创建了一个邮箱,然后创建信件SOAPEnvelope,之后是SOAP的BODY和Header部分,其中Header部分可以省略。不过现在的API可以省略SOAPPart,直接创建BODY和Header,鉴于Part是经典方式,所以还是按照传统来了。
这里涉及到SOAP消息的格式,很形象的可以理解为一封信件,鉴于是基础知识,这里便不再赘述,不清楚的可以先补充一下SOAP格式,理解了SOAP的格式之后,上面那段代码是很容易理解的。
这样进行运行后,我们便可以得到一个完整的SOAP消息:
得到这样的SOAP消息后,怎么与我们发布的WSDL进行交互呢?
7、SOAP消息传递和处理,使用Dispatch
package zmx.jaxws.soap;import java.net.URL;import javax.xml.namespace.QName;import javax.xml.soap.MessageFactory;import javax.xml.soap.SOAPBody;import javax.xml.soap.SOAPBodyElement;import javax.xml.soap.SOAPEnvelope;import javax.xml.soap.SOAPMessage;import javax.xml.soap.SOAPPart;import javax.xml.ws.Dispatch;import javax.xml.ws.Service;import org.junit.Test;import org.w3c.dom.Document;public class SOAPTest {private String ns = "http://server.jaxws.zmx/";private String wsdlUrl = "http://localhost:6666/ws/myService?wsdl";/** * 创建SOAP消息 * @author zwx */@Test public void createSoap() {try {// 创建消息工厂MessageFactory factory = MessageFactory.newInstance();// 根据消息工厂创建SoapMessageSOAPMessage message = factory.createMessage();// 创建SOAPPartSOAPPart part = message.getSOAPPart();// 获取SOAPEnvelopeSOAPEnvelope envelope = part.getEnvelope(); // 通过SoapEnvelope可以获取到相应的Body和Header等信息SOAPBody body = envelope.getBody();// 根据Qname创建相应的节点,Qname是一个带有命名空间的节点QName qname = new QName("http://server.jaxws.zmx/", "add", "ns");// <ns:add xmlns="http://java.wh/webservice/"SOAPBodyElement ele = body.addBodyElement(qname);ele.addChildElement("a").setValue("11");ele.addChildElement("b").setValue("22");// 打印消息信息message.writeTo(System.out);} catch (Exception e) {e.printStackTrace();}}/** * 创建SOAP消息,并提交得到返回值 * @author WHuan */@Testpublic void soapDispatch() {try {// 创建服务serviceURL url = new URL(wsdlUrl);QName sname= new QName(ns,"MyServiceImplService");Service service =Service.create(url,sname);//创建DIspatchDispatch<SOAPMessage> dispatch=service.createDispatch(new QName(ns,"MyServiceImplPort"), SOAPMessage.class, Service.Mode.MESSAGE);//创建SOAPMessageSOAPMessage msg=MessageFactory.newInstance().createMessage();SOAPEnvelope envelope =msg.getSOAPPart().getEnvelope();SOAPBody body=envelope.getBody();//创建QName来指定消息中传递数据QName ename=new QName(ns,"add","nn");//<nn:add xmlns="xx"/>SOAPBodyElement ele=body.addBodyElement(ename);ele.addChildElement("firstA").setValue("22");ele.addChildElement("SecondB").setValue("33");msg.writeTo(System.out);System.out.println("\n invoking....");//通过Dispatch传递消息,会返回相应消息SOAPMessage response = dispatch.invoke(msg);response.writeTo(System.out);System.out.println();//将相应的消息转换为doc对象Document doc= response.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();String str=doc.getElementsByTagName("addResult").item(0).getTextContent();System.out.println(str);} catch (Exception e) {e.printStackTrace();}}}
这样直接创建了SOAP消息后使用dispatch便可以进行传递,通过extractConentAsDocument方法得到Document类型的返回值,从返回值的SOAP消息中取得我们想要的标签信息。输出结果如下:
本文主要从使用JAVA自带API出发,介绍一些稍微底层些的SOAP消息的创建和传递处理。理解了底层,也对web Service的框架使用多一份熟悉,也当给大家复习下SOAP消息。
- JAX-WS创建和JavaAPI发送和处理SOAP消息实例
- JAX-WS创建和处理SOAP消息
- JAX-WS创建和处理SOAP消息
- JAX-WS创建和处理SOAP消息
- JAX-WS - Soap消息的捕获
- JAX-WS - Soap消息的捕获
- jax-ws创建和访问webservice
- jax-ws 创建服务端和客户端
- 【JAX-WS入门系列】第03章_使用SAAJ创建SOAP消息
- JAX-WS - Soap详解
- JAX-WS - Soap详解
- jax-ws服务端生成和客户端调用实例详解
- JAX-WS中通过Handler操作SOAP消息
- SOAP消息的创建、传递和处理(MESSAGE方式)
- JAX-WS使用wsgen和wsimport从Java创建WebService
- 创建webService (jax-ws)的客户端和服务端
- WebService JAX-WS和JAX-RS
- jax-RPC和jax-WS比较
- 第九周项目1(1)
- 函数与指针
- typedef用法
- Runtime的初步认识——消息机制
- 165 php Fatal error: Uncaught SmartyException: unable to write file
- JAX-WS创建和JavaAPI发送和处理SOAP消息实例
- GitHub是什么样的网站 (分享```)
- 静态成员应用 -时间
- OkHttp之Interceptors
- 深度学习基础(十)—— 稀疏编码(二)
- HIBERNATE的缓存机制
- [leetcode] 289. Game of Life 解题报告
- 18个最热深度学习Github项目逐一介绍
- View的构造函数