Create a new Java Project in eclipse first
(Can not WTP dynamic Web Project)
Select Java Project
and look at Pom.xml .
We use the CXF 3.1.4 version,
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" >
<modelVersion>4.0.0</modelVersion>
<groupId>com.cnblog.richaaaard.cxfstudy</groupId>
<artifactId>cxf-test-standalone-ws-helloworld</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>cxf-test-standalone-ws-helloworld Maven webapp</name>
<url>http://maven.apache.org</url>
<properties>
<span style= "color: #ff0000;" ><!--<cxf.version>2.7.18</cxf.version>--></span><br><span style= "COLOR: # ff0000; " ><!--attention to the use of 2.x.x version of the students, the following examples of the introduction of CXF-BUNDLE-JAXRS is unnecessary, because Jaxws<span style= "line-height:1.5;" > Start a service </span><span style= "line-height:1.5;" > Needs class Jaxwsserverfactorybean in Cxf-rt-frontend-jaxws.jar--></span></span>
<cxf.version>3.1.4</cxf.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-security</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-policy</artifactId>
<version>${cxf.version}</version>
</dependency>
<span style= "color: #c0c0c0;" ><!--<dependency>-->
<!--<groupId>org.apache.cxf</groupId>-->
<!--<artifactId>cxf-bundle-jaxrs</artifactId>-->
<!--<version>${cxf.version}</version>-->
<!--</dependency>--></span>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.8</version>
</dependency>
<span style= "color: #ff0000;" > <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.5.8</version>
</dependency> </span>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>cxfstudy</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<configuration>
<contextPath>/</contextPath>
<connectors>
<connector implementation= "Org.mortbay.jetty.nio.SelectChannelConnector" >
<port>9000</port>
</connector>
</connectors>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Another example of SLF4J is the version of Slf4j-jdk14 (HTTP://MVNREPOSITORY.COM/ARTIFACT/ORG.SLF4J/SLF4J-JDK14), If you use Jdk5 above the classmate can replace it with slf4j-simple implementation.
CXF Web Service (Annotation) implementation, on Code
CXF supports two ways to publish a Web service, one is the Java Annotation (Bottom up), the other is to first define the WSDL and XSD schema and then through the tool generation (top down), our example here first introduces Java Annotation.
First we need to define a Web service interface class HelloWorld and add annotation
Package com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services;
Import Javax.jws.WebMethod;
Import Javax.jws.WebParam;
Import Javax.jws.WebResult;
Import Javax.jws.WebService;
@WebService
Public interface HelloWorld {
@WebMethod
@WebResult string Sayhi (@WebParam string text);
}
Here we do not introduce @WebService, @WebMethod, @WebResult and how @webparam work.
Then we need to use Helloworldimpl to implement this interface class,
Package com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services;
public class Helloworldimpl implements HelloWorld {
public string Sayhi (string name) {
String msg = "Hello" + name + "!";
return msg;
}
}
Publish a Web Service
Let's use javax.xml.ws.Endpoint in the simplest way to publish
Package com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.server;
Import Javax.xml.ws.Endpoint;
Import Com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorldImpl;
public class Simpleserver {
public static void Main (string[] args) throws Exception {
System.out.println ("Starting Server");
Helloworldimpl implementor = new Helloworldimpl ();
String address = "Http://localhost:9000/ws/<span style=" color: #ff0000; " >HelloWorld</span> ";
Endpoint.publish (address, implementor);
}
}
Note: The address path here is case-sensitive and if it is published as "Http://localhost:9000/ws/helloWorld", the client will not be able to find the service using/helloworld.
Direct run as.. -> Java Application
We can see in eclipse that a jetty service is running correctly.
Starting Server
128 [main] INFO org.apache.cxf.service.factory.reflectionservicefactorybean-creating service {http:// Services.helloworld.ws.standalone.cxftest.richaaaard.cnblog.com/}helloworldimplservice from class Com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorld
487 [main] INFO org.apache.cxf.endpoint.serverimpl-setting the server ' s publish address to is Http://localhost:9000/ws/H Elloworld
506 [main] INFO org.eclipse.jetty.server.server-jetty-8.1.15.v20140411
554 [main] INFO org.eclipse.jetty.server.abstractconnector-started selectchannelconnector@localhost:9000
At this point, we access Http://localhost:9000/ws/HelloWorld?WSDL through the browser
How do we access this service?
We use the Jaxwsproxyfactorybean in Jaxws to consume this service.
Package com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.client;
Import Org.apache.cxf.interceptor.LoggingInInterceptor;
Import Org.apache.cxf.interceptor.LoggingOutInterceptor;
Import Org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
Import Com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorld;
public class Client {
public static void Main (string[] args) {
Jaxwsproxyfactorybean factory = new Jaxwsproxyfactorybean ();
<span style= "color: #ff0000;" > Factory.getininterceptors (). Add (New Loggingininterceptor ());
Factory.getoutinterceptors (). Add (New Loggingoutinterceptor ());</span>
Factory.setserviceclass (Helloworld.class);
Factory.setaddress ("Http://localhost:9000/ws/HelloWorld");
HelloWorld HelloWorld = (HelloWorld) factory.create ();
String reply = Helloworld.sayhi ("HI");
SYSTEM.OUT.PRINTLN ("Server said:" + reply);
System.exit (0);
}
}
Also right-click to select Client.java,run as ... ->java application, we can see that the log running behind the scenes has finally successfully returned to the Server Said:hello hi!
114 [main] INFO org.apache.cxf.service.factory.reflectionservicefactorybean-creating service {http:// Services.helloworld.ws.standalone.cxftest.richaaaard.cnblog.com/}helloworldservice from class Com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorld
531 [main] INFO Org.apache.cxf.services.helloworldservice.helloworldport.helloworld-outbound message
---------------------------
Id:1
Address:http://localhost:9000/ws/helloworld
Encoding:utf-8
Http-method:post
Content-type:text/xml
Headers: {accept=[*/*], soapaction=[""]}
Payload: <soap:envelope xmlns:soap= "http://schemas.xmlsoap.org/soap/envelope/" ><SOAP:BODY><NS2: Sayhi xmlns:ns2= "http://services.helloworld.ws.standalone.cxftest.richaaaard.cnblog.com/" ><arg0>HI< /arg0></ns2:sayhi></soap:body></soap:envelope>
--------------------------------------
748 [main] INFO Org.apache.cxf.services.helloworldservice.helloworldport.helloworld-inbound message
----------------------------
Id:1
response-code:200
Encoding:utf-8
Content-type:text/xml;charset=utf-8
Headers: {content-length=[258], content-type=[text/xml;charset=utf-8], Server=[jetty (8.1.15.v20140411)]}
Payload: <soap:envelope xmlns:soap= "http://schemas.xmlsoap.org/soap/envelope/" ><SOAP:BODY><NS2: Sayhiresponse xmlns:ns2= "http://services.helloworld.ws.standalone.cxftest.richaaaard.cnblog.com/" ><return >hello hi!</return></ns2:sayhiresponse></soap:body></soap:envelope>
--------------------------------------
Server Said:hello hi!
The above outbound message and inbound message are the two logging interceptor output logs we add, with a presentation on the principles of interceptor and more ways to use them.
This completes the publication and consumption of a Web service
* Service-side extension
In addition to using one of the simplest javax.xml.ws.Endpoint to publish the CXF Web service, We can also use the Jaxwsserverfactorybean in Cxf-rt-frontend-jaxws.jar to publish a Web service that allows us to more control the behavior of Web service, such as adding logging Interceptor or something.
Package com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.server;
Import Org.apache.cxf.interceptor.LoggingInInterceptor;
Import Org.apache.cxf.interceptor.LoggingOutInterceptor;
Import Org.apache.cxf.jaxws.JaxWsServerFactoryBean;
Import Com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorld;
Import Com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorldImpl;
public class Server {
public static void Main (string[] args) throws Exception {
Jaxwsserverfactorybean factory = new Jaxwsserverfactorybean ();
Factory.setserviceclass (Helloworld.class);
Helloworldimpl implementor = new Helloworldimpl ();
Factory.setservicebean (implementor);
<span style= "color: #ff0000;" > Factory.getininterceptors (). Add (New Loggingininterceptor ());
Factory.getoutinterceptors (). Add (New Loggingoutinterceptor ());</span>
Factory.setaddress ("Http://localhost:9000/ws/HelloWorld");
Factory.create ();
System.out.println ("Server start ...");
Thread.Sleep (60 * 1000);
System.out.println ("Server exit ...");
System.exit (0);
}
}
CXF Implementation of WEB service (Spring consolidation method)
To create a new Java Web project and use MAVEN to manage the jar package, this article simply provides the core code files as follows:
(1) The Pom.xml of Maven
(2) Cxf-base.xml
(3) Cxf-bz-client.xml Client Configuration
(4) Cxf-bz-server.xml service-side configuration
(5) Wsorderservice Business Interface Definition
(6) Wsorderserviceimpl Business Interface Implementation
(7) Bzclienthandler,bzservicehandler user authentication Intercept
(8) Web.xml Project Web Configuration
(9) Demo.properties URL and User configuration information
Note: The spring-related configuration file is not listed.
1 The main core pom.xml configuration is as follows:
<!--Spring Framework-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!--end of Spring framework-->
<!--CXF dependencies-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-common</artifactId>
<version>2.5.4</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-core</artifactId>
<version>${cxf.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<!--end of CXF dependencies-->
(2) Cxf-base.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xmlns:jaxws= "Http://cxf.apache.org/jaxws"
xmlns:http-conf= "Http://cxf.apache.org/transports/http/configuration"
Xsi:schemalocation= "
Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://cxf.apache.org/jaxws
Http://cxf.apache.org/schemas/jaxws.xsd
Http://cxf.apache.org/transports/http/configuration
Http://cxf.apache.org/schemas/configuration/http-conf.xsd ">
<import resource= "Classpath:meta-inf/cxf/cxf.xml"/>
<import resource= "Classpath:meta-inf/cxf/cxf-extension-soap.xml"/>
<import resource= "Classpath:meta-inf/cxf/cxf-servlet.xml"/>
<!--docking Web service configuration-->
<import resource= "Classpath:conf/cxf/cxf-bz-server.xml"/>
<import resource= "Classpath:conf/cxf/cxf-bz-client.xml"/>
</beans>
(3) Cxf-bz-client.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xmlns:tx= "Http://www.springframework.org/schema/tx"
xmlns:context= "Http://www.springframework.org/schema/context"
Xmlns:jaxws= "Http://cxf.apache.org/jaxws"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
Http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd "
Default-lazy-init= "true" >
<!---->
<bean id= "Bzclientsoaphandler" class= "Com.lshop.ws.util.BZClientHandler" >
<property name= "AccountNum" value= "${bz.ws.account}" ></property>
<property name= "Password" value= "${bz.ws.password}" ></property>
</bean>
<!---->
<jaxws:client id= "Bzexpressservice" name= "Bzexpressservice" Com.lshop.ws.web.bz.order.BzExpressService "wsdllocation=" ${bz.sys.url}/ws/wsorderservice?wsdl ">
<jaxws:handlers>
<ref bean= "Bzclientsoaphandler"/>
</jaxws:handlers>
</jaxws:client>
</beans>
(4) Cxf-bz-server.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xmlns:tx= "Http://www.springframework.org/schema/tx"
xmlns:context= "Http://www.springframework.org/schema/context"
Xmlns:jaxws= "Http://cxf.apache.org/jaxws"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
Http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd "
Default-lazy-init= "true" >
<!---->
<bean id= "Bzserversoaphandler" class= "Com.lshop.ws.util.BZServiceHandler" >
<property name= "AccountNum" value= "${bz.ws.account}" ></property>
<property name= "Password" value= "${bz.ws.password}" ></property>
</bean>
<jaxws:server serviceclass= "Com.lshop.ws.server.bz.order.service.WSOrderService" address= "/wsorderservice" >
<jaxws:handlers>
<ref bean= "Bzserversoaphandler"/>
</jaxws:handlers>
<jaxws:serviceBean>
<ref bean= "Wsorderservice"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>
(5) Wsorderservice defines a business interface
Package com.lshop.ws.server.bz.order.service;
Import Java.util.Date;
Import Javax.jws.WebParam;
Import Javax.jws.WebService;
Import javax.jws.soap.SOAPBinding;
Import Javax.jws.soap.SOAPBinding.Style;
Import Com.lshop.ws.server.bz.order.bean.LvOrderDtoResposne;
@WebService (name = "Wsorderservice")
@SOAPBinding (STYLE=STYLE.RPC)
Public interface Wsorderservice {
/**
* Synchronize the approved orders to the business interface definition
* @Method: Synauditordertobz
* @Description:
* @param starttime Audit start time
* @param endtime Audit settlement time
* @return OrderList
*/
Public Lvorderdtoresposne Synauditordertobz (
@WebParam (name = "StartTime") String StartTime,
@WebParam (name = "Endtime") String Endtime);
}
(6) Wsorderserviceimpl Business Implementation Class
Package com.lshop.ws.server.bz.order.service;
Import java.util.ArrayList;
Import Java.util.Date;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import Javax.annotation.Resource;
Import Javax.jws.WebService;
Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;
Import Org.springframework.stereotype.Service;
Import Com.gv.core.hibernate3.HibernateBaseDAO;
Import Com.gv.core.service.impl.BaseServiceImpl;
Import Com.gv.core.util.ObjectUtils;
Import Com.lshop.common.pojo.logic.LvOrderDetails;
Import Com.lshop.common.pojo.logic.LvOrderPackageDetails;
Import Com.lshop.common.util.DateUtils;
Import Com.lshop.ws.server.bz.order.bean.LvOrderDto;
Import Com.lshop.ws.server.bz.order.bean.LvOrderDtoResposne;
Import Com.lshop.ws.server.bz.order.bean.LvProductDto;
Import Com.lshop.ws.server.bz.order.bean.Result;
@Service ("Wsorderservice")
@WebService (endpointinterface = "Com.lshop.ws.server.bz.order.service.WSOrderService")
public class Wsorderserviceimpl extends Baseserviceimpl implements Wsorderservice {
@Resource
Private Hibernatebasedao DAO;
Private static final Log logger = Logfactory.getlog (Wsorderserviceimpl.class);
/**
* Synchronize the approved orders to the business interface definition
* @Method: Synauditordertobz
* @Description:
* @param starttime Audit Start time (query range start time, format: Yyyy-mm-dd HH:mm:ss)
* @param endtime Audit End time (query range end time, format: Yyyy-mm-dd HH:mm:ss)
* @return OrderList
*/
@Override
Public Lvorderdtoresposne Synauditordertobz (string starttime, String endtime) {
if (logger.isinfoenabled ()) {
Logger.info ("*****wsorderserviceimpl.synauditordertobz () method begin*****");
}
Lvorderdtoresposne lvorderdtoresposne=new Lvorderdtoresposne ();
List<lvorderdto> orderlist=new arraylist<lvorderdto> ();
Determine Query date time difference
Query approved order collection based on audit time
try {
if (Objectutils.isnotempty (starttime) &&objectutils.isnotempty (Endtime)) {
String hql= "Select DISTINCT Ls.ord as ord from Lvorderlogs ls,lvorder o" +
"Where ls.ord=o.oid" +
"and o.isserviceaudit=1" +
"and o.isfinanceaudit=1" +
"and o.isdelete=0" +
"and Ls.createtime>=:starttime and Ls.createtime <=:endtime";
Map param=new HashMap ();
Param.put ("StartTime", Dateutils.converttodatetime (StartTime));
Param.put ("Endtime", Dateutils.converttodatetime (Endtime));
List listtmp = dao.getmaplistbyhql (hql, param). GetList ();
if (Objectutils.isnotempty (listtmp)) {
for (int i = 0; i < listtmp.size (); i++) {
Map map= (map) listtmp.get (i);
Query Order Information
Lvorderdto Lvorderdto=this.getorderinfo (string.valueof (Map.get ("Ord"));
Orderlist.add (lvorderdto);
}
}
}
Lvorderdtoresposne.setorderlist (orderlist);
Lvorderdtoresposne.getresult (). SetStatus (Result.status_succeed);
Lvorderdtoresposne.getresult (). Setmessage ("Success");
catch (Exception e) {
Lvorderdtoresposne.getresult (). SetStatus (Result.status_fail);
Lvorderdtoresposne.getresult (). Setmessage (E.getmessage ());
E.printstacktrace ();
}
if (logger.isinfoenabled ()) {
Logger.info ("*****wsorderserviceimpl.synauditordertobz () method end*****");
}
return lvorderdtoresposne;
}
/**
*
* @Method: Getorderdetailsbyoid
* @Description: Search Order details and corresponding product information according to the order number
* @param oid Order number
* @return list<lvproductdto>
*/
Protected list<lvproductdto> getorderdetailsbyoid (String oid) {
return null;
}
/**
*
* @Method: GetOrderInfo
* @Description: Search order based on order number
* @param oid Order number
* @return list<lvproductdto>
*/
Protected Lvorderdto GetOrderInfo (String oid) {
return null;
}
}
(7) Bzclienthandler,bzservicehandler user authentication interception.
Package com.lshop.ws.util;
Import java.io.IOException;
Import Java.util.Set;
Import Javax.xml.namespace.QName;
Import Javax.xml.soap.SOAPBody;
Import javax.xml.soap.SOAPElement;
Import Javax.xml.soap.SOAPEnvelope;
Import javax.xml.soap.SOAPException;
Import Javax.xml.soap.SOAPFault;
Import Javax.xml.soap.SOAPHeader;
Import Javax.xml.soap.SOAPMessage;
Import Javax.xml.ws.handler.MessageContext;
Import Javax.xml.ws.handler.soap.SOAPHandler;
Import Javax.xml.ws.handler.soap.SOAPMessageContext;
Import javax.xml.ws.soap.SOAPFaultException;
public class Bzclienthandler implements soaphandler<soapmessagecontext> {
Private String AccountNum;
private String password;
Public String Getaccountnum () {
return AccountNum;
}
public void Setaccountnum (String AccountNum) {
This.accountnum = AccountNum;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
@Override
public boolean handlemessage (Soapmessagecontext context) {
System.out.println ("Server:handlemessage () ...");
try{
SoapMessage soapmsg = Context.getmessage ();
SoapEnvelope soapenv = Soapmsg.getsoappart (). Getenvelope ();
SoapHeader SoapHeader = Soapenv.getheader ();
If no header, add one
if (SoapHeader = = null) {
SoapHeader = Soapenv.addheader ();
}
SoapElement Accountnumnode = soapheader.addchildelement (New QName ("http:// service.webservice.datainterface.bz.gv.com/"," AccountNum "));
Accountnumnode.setnodevalue ("AccountNum");
Accountnumnode.setvalue (AccountNum);
SoapElement Passwordnode = soapheader.addchildelement (New QName ("http://service.webservice.datainterface.bz.gv.com /"," password "));
Passwordnode.setnodevalue ("password");
Passwordnode.setvalue (password);
Soapmsg.writeto (System.out);
return true;
}catch (SoapException e) {
System.err.println (e);
}catch (IOException e) {
System.err.println (e);
}
Continue other handler chain
return false;
}
@Override
public boolean Handlefault (Soapmessagecontext context) {
System.out.println ("Server:handlefault () ...");
return true;
}
@Override
public void Close (Messagecontext context) {
System.out.println ("Server:close () ...");
}
@Override
Public set<qname> getheaders () {
System.out.println ("Server:getheaders () ...");
return null;
}
private void Generatesoaperrmessage (SoapMessage msg, String reason) {
try {
Soapbody soapbody = Msg.getsoappart (). Getenvelope (). GetBody ();
SoapFault SoapFault = Soapbody.addfault ();
soapfault.setfaultstring (reason);
throw new Soapfaultexception (SoapFault);
}
catch (SoapException e) {}
}
}
(8) Web.xml Configure the Web Servcie request to intercept the URL.
<!--Note: Just keep the key configuration and omit other configuration information-->
<!--Web Service config begin-->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
<!--Web Service config end-->
(9) Demo.properties
bz.sys.url=http\://127.0.0.1\:8080
Bz.ws.account=demo
bz.ws.password=demo400
Configuration completion can be accessed through the following server-side URL:
http://127.0.0.1:8080/ws/WSOrderService?wsdl
Client-side Java files, in the call interface implementation can.