Preferential contract development and implicit declaration header information in cxf

Source: Internet
Author: User
Tags wsdl

In fact, it is basically the same with the JAX-WS contract priority development, see the http://blog.csdn.net/jadyer/article/details/9002218

The difference is that two parameters are added when the cxf is released.

First, check the server code. This is a Java project.

First is the // SRC // META-INF // calculator. XSD used to define the element type

<?xml version="1.0" encoding="UTF-8"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"targetNamespace="http://blog.csdn.net/jadyer"xmlns:tns="http://blog.csdn.net/jadyer"elementFormDefault="unqualified"><xsd:element name="add" type="tns:add"/><xsd:element name="addResponse" type="tns:addResponse"/><xsd:element name="licenseUser" type="tns:user"/><xsd:complexType name="add"><xsd:sequence><xsd:element name="a" type="xsd:int"/><xsd:element name="b" type="xsd:int"/></xsd:sequence></xsd:complexType><xsd:complexType name="addResponse"><xsd:sequence><xsd:element name="addResult" type="xsd:int"/></xsd:sequence></xsd:complexType><xsd:complexType name="user"><xsd:sequence><xsd:element name="username" type="xsd:string"/><xsd:element name="password" type="xsd:string"/></xsd:sequence></xsd:complexType></xsd:schema>

Next we write our own // SRC // META-INF // mycalculator. WSDL

<?xml version="1.0" encoding="UTF-8"?><wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:tns="http://blog.csdn.net/jadyer"targetNamespace="http://blog.csdn.net/jadyer"name="CalculatorServiceImpl"><wsdl:types><xsd:schema targetNamespace="http://blog.csdn.net/jadyer"><xsd:include schemaLocation="calculator.xsd"/></xsd:schema></wsdl:types><wsdl:message name="add"><wsdl:part name="add" element="tns:add"/></wsdl:message><wsdl:message name="addResponse"><wsdl:part name="addResponse" element="tns:addResponse"/></wsdl:message><wsdl:message name="licenseUser"><wsdl:part name="licenseUser" element="tns:licenseUser"/></wsdl:message><wsdl:portType name="CalculatorService"><wsdl:operation name="add"><wsdl:input message="tns:add"/><wsdl:output message="tns:addResponse"/></wsdl:operation></wsdl:portType><wsdl:binding name="CalculatorServiceImplPortBinding" type="tns:CalculatorService"><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="licenseUser" message="tns:licenseUser"/></wsdl:input><wsdl:output><soap:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="CalculatorServiceImpl"><wsdl:port binding="tns:CalculatorServiceImplPortBinding" name="CalculatorServiceImplPort"><soap:address location="http://127.0.0.1:8088/myCalculatorService"/></wsdl:port></wsdl:service></wsdl:definitions>

The following is a self-compiled SIB, that is, the server interface implementation class.

package net.csdn.blog.jadyer;import javax.jws.WebService;import com.jadyer.model.User;@WebService(serviceName="CalculatorServiceImpl",wsdlLocation="META-INF/myCalculator.wsdl",endpointInterface="net.csdn.blog.jadyer.CalculatorService",targetNamespace="http://blog.csdn.net/jadyer")public class CalculatorServiceImpl implements CalculatorService {@Overridepublic int add(int a, int b, User licenseUser) {if(null != licenseUser){System.out.println("Receive the username=[" + licenseUser.getUsername() + "]");System.out.println("Receive the password=[" + licenseUser.getPassword() + "]");}System.out.println(a + "+" + b + "=" + (a+b));return a+b;}}

The following is an entity class used by the server.

Package COM. jadyer. model; public class user {private string username; private string password;/* the setter and getter attributes are omitted */public user () {} public user (string username, string password) {This. username = username; this. password = password ;}}

Finally, the endpoint used by the server to publish services

Package COM. jadyer. server; import javax. XML. namespace. QNAME; import net. csdn. blog. jadyer. calculatorservice; import net. csdn. blog. jadyer. calculatorserviceimpl; import Org. apache. cxf. interceptor. loggingininterceptor; import Org. apache. cxf. interceptor. loggingoutinterceptor; import Org. apache. cxf. jaxws. jaxwsserverfactorybean;/*** contract priority development in cxf and implicit declaration header information * @ see references * @ see development process * @ see 1) create and write \ SRC \ META-INF \ calculator. XSD and mycalculator. the WSDL file * @ see 2) generates server code (wsdl2java-d:/download/-frontend jaxws21-keep-verbose mycalculator. WSDL) * @ see it generates a lot of code. As a server, you only need to keep the SEI ............ if it is a client, you cannot delete * @ see and then add @ xmlseealso ({objectfactory. class}) and classname = "..... "Delete in two places * @ see 3) compile implementation class * @ see specify @ WebService (servicename =" ", wsdllocation =" ", endpointinterface = "", targetnamespace = "") * @ see 4) the address of the Publishing Service * @ see can be specified arbitrarily. It is not required to be associated with mycalculator. <soap: address location = ""/> in WSDL is the same * @ see. However, when you view the WSDL in a browser, you will find that <soap: the address location = ""/> value is always the same as the address specified at release * @ see references * @ create may 28,201 3 9:41:27 pm * @ author Xuan Yu 

OK. The server code example is complete. The following is the client code.

First, interceptor customized by the client for sending soapheader Information

package com.jadyer.interceptor;import javax.xml.bind.JAXBException;import javax.xml.namespace.QName;import net.csdn.blog.jadyer.User;import org.apache.cxf.binding.soap.SoapMessage;import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;import org.apache.cxf.databinding.DataBinding;import org.apache.cxf.headers.Header;import org.apache.cxf.interceptor.Fault;import org.apache.cxf.jaxb.JAXBDataBinding;import org.apache.cxf.phase.Phase;public class LicenseOutInterceptor extends AbstractSoapInterceptor{public LicenseOutInterceptor(){super(Phase.WRITE);}@Overridepublic void handleMessage(SoapMessage message) throws Fault {QName qname = new QName("http://blog.csdn.net/jadyer", "licenseUser", "ns");DataBinding dataBinding = null;try {dataBinding = new JAXBDataBinding(User.class);} catch (JAXBException e) {e.printStackTrace();}User user = new User();user.setUsername("Jadyer");user.setPassword("hongyu");Header header = new Header(qname, user, dataBinding);message.getHeaders().add(header);}}

Finally, the simulated entrance for the client to call the server

Note: The specific client WebService code is generated by wsdl2java.

Package COM. jadyer. client; import net. csdn. blog. jadyer. calculatorservice; import Org. apache. cxf. interceptor. loggingininterceptor; import Org. apache. cxf. interceptor. loggingoutinterceptor; import Org. apache. cxf. jaxws. jaxwsproxyfactorybean; import COM. jadyer. interceptor. licenseoutinterceptor; public class clientapp {public static void main (string [] ARGs) {// wsdl2java-d:/download/-frontend jaxws21-keep -Verbose http: // 127.0.0.1: 8088/mycalculator? WSDL // wsdl2java commands are similar to wsimport. For more information about wsimport, see http://blog.csdn.net/jadyer/article/details/8692108JaxWsProxyFactoryBean factory = new jaxwsproxyfactorybean (); factory. setaddress ("http: // 127.0.0.1: 8088/mycalculator"); factory. setserviceclass (calculatorservice. class); factory. getininterceptors (). add (New loggingininterceptor (); factory. getoutinterceptors (). add (New loggingoutinterceptor (); factory. getoutinterceptors (). add (New licenseoutinterceptor (); calculatorservice Hello = (calculatorservice) factory. create (); system. out. println (hello. add (12, 33 ));}}

Old habits, finally paste the console output

This is output from the server console.

23:06:19 Org. apache. cxf. services. calculatorserviceimpl. calculatorserviceimplport. calculatorservice information: Inbound message -------------------------- ID: 1 address: http: // 127.0.0.1: 8088/mycalculatorencoding: UTF-8Http-Method: postcontent-type: text/XML; charset = UTF-8Headers: {accept = [*/*], cache-control = [no-Cache], connection = [keep-alive], Content-Length = [342], content-Type = [text/XML; charset = UTF-8], host = [127.0.0.1: 8088], Pragma = [no-Cache], soapaction = [""], user-Agent = [Apache cxf 2.7.0]} payload: <soap: envelope xmlns: Soap = "http://schemas.xmlsoap.org/soap/envelope/"> <soap: Header> <NS2: licenseuser xmlns: <username> jadyer </username> <password> Hongyu </password> </NS2: licenseuser> </soap: Header> <soap: body> <NS2: Add xmlns: NS2. = "http://blog.csdn.net/jadyer"> <A> 12 </a> <B> 33 </B> </int32: Add> </soap: body> </soap: envelope> -------------------------------------- receive the username = [jadyer] receive the Password = [Hongyu] 12 + 33 = 452013-5-31 23:06:19 Org. apache. cxf. services. calculatorserviceimpl. calculatorserviceimplport. calculatorservice information: Outbound message ------------------------- ID: 1 encoding: UTF-8Content-Type: text/xmlheaders :{} payload: <soap: envelope xmlns: Soap = "http://schemas.xmlsoap.org/soap/envelope/"> <soap: body> <NS2: addresponse xmlns: NS2. = "http://blog.csdn.net/jadyer"> <addresult> 45 </addresult> </ns: addresponse> </soap: Body> </soap: envelope> --------------------------------------

This is the client console output

23:06:18 Org. apache. cxf. service. factory. reflectionservicefactorybean buildservicefromclass information: creating service {http://blog.csdn.net/jadyer#calculatorserviceservice from class net. csdn. blog. jadyer. calculatorService2013-5-31 23:06:19 Org. apache. cxf. services. calculatorserviceservice. calculatorserviceport. calculatorservice information: Outbound message ------------------------- ID: 1 address: http: // 127.0.0.1: 8088/mycalculatorencoding: UTF-8Http-Method: postcontent-type: text/xmlheaders: {accept = [*/*], soapaction = [""]} payload: <soap: envelope xmlns: Soap = "http://schemas.xmlsoap.org/soap/envelope/"> <soap: Header> <NS2: licenseuser xmlns: <username> jadyer </username> <password> Hongyu </password> </NS2: licenseuser> </soap: Header> <soap: body> <NS2: Add xmlns: NS2. = "http://blog.csdn.net/jadyer"> <A> 12 </a> <B> 33 </B> </int32: Add> </soap: body> </soap: envelope> ---------------------------------------- 23:06:19 Org. apache. cxf. services. calculatorserviceservice. calculatorserviceport. calculatorservice information: Inbound message -------------------------- ID: 1response-code: 200 encoding: UTF-8Content-Type: text/XML; charset = UTF-8Headers: {Content-Type = [text/XML; charset = UTF-8], server = [Jetty (8.1.7.v20120910)], transfer-encoding = [chunked]} payload: <soap: envelope xmlns: Soap = "http://schemas.xmlsoap.org/soap/envelope/"> <soap: Body> <NS2: addresponse xmlns: ns2-= "http://blog.csdn.net/jadyer"> <addresult> 45 </addresult> </ns2-: addresponse> </soap: Body> </soap: envelope> ------------------------------ 45

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.