契约优先方式编写wsdl + 隐式Header

来源:互联网 发布:js广告代码生成器 编辑:程序博客网 时间:2024/05/22 16:45

hello.wsdl

第一步:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:tns="http://www.example.org/hello/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"xmlns:xsd="http://www.w3.org/2001/XMLSchema"name="HelloService"targetNamespace="http://www.example.org/hello/"><wsdl:types><xsd:schema targetNamespace="http://www.example.org/hello/"><xsd:element name="add" type="tns:add"></xsd:element><xsd:element name="addResponse" type="tns:addResponse"></xsd:element><xsd:element name="licenceInfo" type="tns:licenceInfo"></xsd:element><xsd:complexType name="add"><xsd:sequence><xsd:element name="a" type="xsd:int"></xsd:element><xsd:element name="b" type="xsd:int"></xsd:element></xsd:sequence></xsd:complexType><xsd:complexType name="addResponse"><xsd:sequence><xsd:element name="addResponse" type="xsd:int"></xsd:element></xsd:sequence></xsd:complexType><xsd:complexType name="licenceInfo"><xsd:sequence><xsd:element name="licenceInfo" type="xsd:string"></xsd:element></xsd:sequence></xsd:complexType></xsd:schema></wsdl:types><wsdl:message name="add"><wsdl:part name="add" element="tns:add"></wsdl:part></wsdl:message><wsdl:message name="addResponse"><wsdl:part name="addResponse" element="tns:addResponse"></wsdl:part></wsdl:message><wsdl:message name="licenceInfo"><wsdl:part name="licenceInfo" element="tns:licenceInfo"></wsdl:part></wsdl:message><wsdl:portType name="Hello"><wsdl:operation name="add"><wsdl:input message="tns:add"></wsdl:input><wsdl:output message="tns:addResponse"></wsdl:output></wsdl:operation></wsdl:portType><wsdl:binding name="HelloService" type="tns:Hello"><soap:binding style="document"transport="http://schemas.xmlsoap.org/soap/http" /><wsdl:operation name="add"><wsdl:input><soap:body use="literal" /><soap:header use="literal" part="licenceInfo" message="tns:licenceInfo"></soap:header></wsdl:input><wsdl:output><soap:body use="literal" /></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="HelloService"><wsdl:port binding="tns:HelloService" name="HelloServicePort"><soap:address location="http://localhost:8080/hello" /></wsdl:port></wsdl:service></wsdl:definitions>

第二步:

通过wsimport  生成 java文件

将生成的java类文件复制到项目中。


第三步:

修改Hello接口文件。

package org.example.hello;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;import javax.xml.bind.annotation.XmlSeeAlso;import javax.xml.ws.RequestWrapper;import javax.xml.ws.ResponseWrapper;/** * This class was generated by the JAX-WS RI. * JAX-WS RI 2.1.1 in JDK 6 * Generated source version: 2.1 *  */@WebService(name = "Hello", targetNamespace = "http://www.example.org/hello/")@XmlSeeAlso({    ObjectFactory.class})public interface Hello {    /**     *      * @param b     * @param a     * @return     *     returns int     */    @WebMethod    @WebResult(name = "addResponse", targetNamespace = "")    @RequestWrapper(localName = "add", targetNamespace = "http://www.example.org/hello/", className = "org.example.hello.Add")    @ResponseWrapper(localName = "addResponse", targetNamespace = "http://www.example.org/hello/", className = "org.example.hello.AddResponse")    public int add(        @WebParam(name = "a", targetNamespace = "")        int a,        @WebParam(name = "b", targetNamespace = "")        int b ,         @WebParam(name="licenceInfo" , header=true) String licenceInfo);}


第四步:

编写HelloServiceImil类 实现Hello接口

@WebService(endpointInterface = "org.example.hello.Hello", serviceName = "HelloService" , wsdlLocation = "META-INF/wsdl/hello.wsdl" ,  targetNamespace = "http://www.example.org/hello/" , portName="HelloServicePort")public class HelloServiceImpl implements Hello {@Overridepublic int add(int a, int b, String licenceInfo) {System.out.printf("%d + %d = %d\n" , a , b , a+b);System.out.println("licenceInfo:" + licenceInfo );return a + b;}}


第五步:

发布service

package com.zf.test;import javax.xml.ws.Endpoint;import org.example.hello.HelloServiceImpl;public class ServiceTest {public static void main(String[] args) {Endpoint.publish("http://localhost:8080/hello", new HelloServiceImpl());}}


第六步:编写客户端。

首先将wsimort生成的java类添加到客户端。


1)、 普通方式访问。(没有头信息)

package com.zf.test;import java.net.URL;import javax.xml.namespace.QName;import javax.xml.ws.Service;import org.example.hello.Hello;public class Client01 {public static void main(String[] args) throws Exception{URL url = new URL("http://localhost:8888/hello?wsdl");String namespace = "http://www.example.org/hello/";QName sname = new QName(namespace , "HelloService");Service service = Service.create(url, sname);Hello hello = service.getPort(Hello.class);int result = hello.add(1, 100);System.out.println(result);}}

2)、SOAPMessage方式访问 。 (有头信息)

package com.zf.test;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.SOAPHeader;import javax.xml.soap.SOAPMessage;import javax.xml.ws.Dispatch;import javax.xml.ws.Service;public class Client02 {/** * 带Header的请求 * @param args * @throws Exception */public static void main(String[] args) throws Exception{URL url = new URL("http://localhost:8888/hello?wsdl");String namespace = "http://www.example.org/hello/";QName sname = new QName(namespace , "HelloService");Service service = Service.create(url, sname);QName protName = new QName(namespace , "HelloServicePort");Dispatch<SOAPMessage> dispatch = service.createDispatch( protName , SOAPMessage.class, Service.Mode.MESSAGE);SOAPMessage msg = MessageFactory.newInstance().createMessage() ;SOAPEnvelope env = msg.getSOAPPart().getEnvelope() ;SOAPBody body = env.getBody() ;SOAPHeader header = env.getHeader() ;if(header == null) header = env.addHeader() ;QName addName = new QName(namespace , "add" , "ns");//一定要加ns前缀SOAPBodyElement bodyEle = body.addBodyElement(addName);//添加Body信息bodyEle.addChildElement("a").setValue("1");bodyEle.addChildElement("b").setValue("2");QName headerName = new QName(namespace , "licenceInfo" );;//添加头信息header.addHeaderElement(headerName).setTextContent("admin");//设置头信息的值msg.writeTo(System.out);//发送前将soap消息打印出来SOAPMessage resultMsg = dispatch.invoke(msg);System.out.println("----------------relult---------------");resultMsg.writeTo(System.out) ;//将返回的soap消息打印出来}}