to facilitate the learning and application of people, or from the configuration files, jars, beans start to pull up ...
1,web.xml configuration file Basic configuration
<web-app version= "2.4" xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_ 2_4.xsd "> <display-name>spring MVC application</display-name> <listener> <listener-class >org.springframework.web.context.ContextLoaderListener</listener-class> </listener> < Context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*: spring-cxf.xml</param-value> </context-param> <servlet> <servlet-name>mvc-dispatcher</ Servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> &L t;load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name> Mvc-dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-Mapping> <!--Spring Integrated CXF Web configuration-<servlet> <servlet-name>CXFServlet</servlet-name> < Servlet-class>org.apache.cxf.transport.servlet.cxfservlet</servlet-class> </servlet> <!-- Cxfservlet Mapping-<servlet-mapping> <servlet-name>CXFServlet</servlet-name> < Url-pattern>/cxf/*</url-pattern> </servlet-mapping> </web-app>
The corresponding relationship of the CXF servlet is configured here, and my intercept path is all URLs under/cxf/.
2,pom.xml configuration file, adding Maven dependencies required for CXF development
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:s chemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>com.springapp</groupId> <artifactid>webcxf</ artifactid> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name> webcxf</name> <properties> <spring.version>4.1.1.RELEASE</spring.version> </PR operties> <dependencies> <dependency> <groupid>org.springframework</group Id> <artifactId>spring-core</artifactId> <version>${spring.version}</versi on> </dependency> <dependency> <groupid>org.springframework</groupid&
Gt <artifactid>spring-web</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactid>servlet-api</
artifactid> <version>2.5</version> </dependency> <dependency>
<groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId>
<version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-w
Ebmvc</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-te
St</artifactid> <version>${spring.version}</version> <scope>test</scope> </depend ency> <dependency> <groupId>junit</groupId> <artifactid>junit& lt;/artifactid> <version>4.11</version> <scope>test</scope> < ;/dependency> <!--CXF Dependent-<!--HTTPS://MVNREPOSITORY.COM/ARTIFACT/ORG.APACHE.CXF/APACHE-CXF -<dependency> <groupId>org.apache.cxf</groupId> <artifactId> apache-cxf</artifactid> <version>3.1.10</version> </dependency> </depen dencies> <build> <finalName>WebCxf</finalName> <plugins> <p
Lugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <ARTIFACTID>MAVEN-SUREFIRE-PLUGIN</ARTIFAC tid> <configuration> <includes> <include> **/*tests.java</include> </includes> </configuration> &L t;/plugin> </plugins> </build> </project>
3, Simple spring config file, not much to say here.
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/ 2001/xmlschema-instance "xmlns:context=" Http://www.springframework.org/schema/context "xmlns:mvc=" http://www . Springframework.org/schema/mvc "xsi:schemalocation=" Http://www.springframework.org/schema/beans http://www . springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context/HTTP Www.springframework.org/schema/context/spring-context.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC/HTTP Www.springframework.org/schema/mvc/spring-mvc.xsd "> <!--Notes Driver--<mvc:annotation-driven/> <co Ntext:component-scan base-package= "com.spring"/> <bean class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "> <property name=" prefix "value="/web-inf /pages/"/> <property name=" suffix "value=". jsp "/> </bean> </beans>
4, for the needs of CXF implementation, add Spring-cxf.xml configuration file, here in Web. XML has been introduced
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:jaxws=" Http://cxf.apache.org/jaxws "xsi:schemalocation=" http://www.spring Framework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd Http://cxf.apa
Che.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd "> <!--Import apache CXF Bean Definition-- <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 "/> <!--analog service side ①webservice--> <bean id= "Hello" class= "Com.spring.service.impl.HelloWordServiceImpl"/> <!--Implemento R= "#hello" point to Bean id= "Hello"--<jaxws:endpoint id= "HelloWorld" implementor= "#hello" address= "/helloworld"/>
; <!--analog service-side ②--> <bean id= "User" class= "Com.spring.service.impl.UserServiceImpl"/> <jaxws:endpoint id= "Userpoint" implementor= "#user" address= " /user "/> <!--analog client ①--> <jaxws:client id=" Client "serviceclass=" Com.spring.service.IH Elloword "address=" Http://localhost:8080/cxf/helloWorld "/> <!--analog client ②--> </beans>
The demo application is this configuration of <!--analog Server ②-->, and the other is not related to the current demo.
5, the implementation of the tool encapsulation for data implementation in XML format.
Package com.spring.utils;
Import org.w3c.dom.Document;
Import org.w3c.dom.Element;
Import Org.w3c.dom.Node;
Import org.w3c.dom.NodeList;
Import Org.xml.sax.InputSource;
Import Javax.xml.parsers.DocumentBuilder;
Import Javax.xml.parsers.DocumentBuilderFactory;
Import Javax.xml.transform.Transformer;
Import Javax.xml.transform.TransformerFactory;
Import Javax.xml.transform.dom.DOMSource;
Import Javax.xml.transform.stream.StreamResult;
Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.io.StringReader;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map; /** * Generate XML-formatted files * Interpret XML-formatted strings * * public class Xmlhandler {/** * Create XML * @return */public Stati
C String Createxml () {string xmlstr= "";
Doc's factory Documentbuilderfactory factory = Documentbuilderfactory.newinstance (); try{Documentbuilder builder = Factory.newdocumentbuiLder ();
Document document = Builder.newdocument ();
Document.setxmlversion ("1.0");
Element root = document.createelement ("root");
Document.appendchild (root);
Element user = document.createelement ("user");
Element id = document.createelement ("id");
Id.settextcontent ("1");
User.appendchild (ID);
Element name = document.createelement ("name");
Name.settextcontent ("Zhang San");
User.appendchild (name);
Element sex = document.createelement ("Sex");
Sex.settextcontent ("male");
User.appendchild (Sex);
Root.appendchild (user);
Transformerfactory transfactory = Transformerfactory.newinstance ();
Transformer Transformer = Transfactory.newtransformer (); Domsource Domsource = new Domsource (document);
Export string Bytearrayoutputstream bos = new Bytearrayoutputstream ();
Transformer.transform (Domsource, New Streamresult (BOS));
Xmlstr = Bos.tostring ();
SYSTEM.OUT.PRINTLN ("output result" +xmlstr);
Outputs the file File = new file ("D:/out/user.xml") through the output stream;
if (!file.exists ()) {file.createnewfile ();
} FileOutputStream out = new FileOutputStream (file);
Streamresult Xmlresult = new Streamresult (out);
Transformer.transform (Domsource, Xmlresult);
}catch (Exception e) {e.printstacktrace ();
} return XMLSTR;
}/*public static void Main (string[] args) {createxml ();
}*//** * Convert XML format string * <?xml version= "1.0" encoding= "UTF-8" standalone= "no"?> * <root> * <user> * <id>1</id> * <name> Zhang San </name> * <sex> Men </sex> * </user> * </root> * * Public Static list<map<string,object>> Parsexml (String xmlstr) {list<map<string,object>> List=nul
L
Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
try{Documentbuilder builder = Factory.newdocumentbuilder ();
StringReader sr = new StringReader (XMLSTR);
InputSource is = new InputSource (SR);
Document doc = Builder.parse (IS);
Element rootelement = Doc.getdocumentelement ();
NodeList users = rootelement.getelementsbytagname ("user");
List=new arraylist<map<string, object>> ();
for (int i=0;i<users.getlength (); i++) {Node node = Users.item (i);
NodeList childs = Node.getchildnodes (); Map<string,object> map=nEW hashmap<string, object> ();
for (int j=0;j<childs.getlength (); j + +) {Node Childnode = Childs.item (j);
String Nodename=childnode.getnodename ();
String value=childnode.gettextcontent ();
Map.put (Nodename,value);
} list.add (map);
}}catch (Exception e) {e.printstacktrace ();
} return list; }/*public static void Main (string[] args) {String xmlstr= "<?xml version= ' 1.0 ' encoding= ' UTF-8 ' Standalon E= ' No '?><root><user><id>1</id><name> Zhang San </name><sex> men </sex>
</user></root> ";
list<map<string, object>> list = Parsexml (XMLSTR);
System.out.println ("======" +list.tostring ());
}*/
}
6, define an interface implementation class, and note the annotations for that class.
Package com.spring.service;
Import Javax.jws.WebParam;
Import Javax.jws.WebService;
/**
* */
@WebService public
interface Iuser {
//Get data in XML format
/**
* Get album information based on passed conditions
* Format specification FOR XML
* <?xml version= "1.0" encoding= "UTF-8" standalone= "no"?>
* <root>
* <user>
* <id>1</id>
* <name> Zhang San </name>
* <sex > Male </sex>
* </user>
* </root>
* Here the Webparam must be specified, otherwise the call will return null
* @return
*
/String GetUser (@WebParam (name = "Xmlstr") string xmlstr);
}
7, implement the interface, pay attention to the application of annotations.
Package Com.spring.service.impl;
Import Com.spring.service.IUser;
Import Com.spring.utils.XMLHandler;
Import Javax.jws.WebService;
Import java.util.List;
Import Java.util.Map;
/**
* */
@WebService public
class Userserviceimpl implements Iuser {
@Override
public String GetUser (String xmlstr) {
list<map<string, object>> List = Xmlhandler.parsexml (XMLSTR);
return list.tostring ();
}
}
8,main method Test.
Package com.spring.test;
Import Org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
Import Javax.xml.namespace.QName; /** * * */public class TESTCLIENTCXF {public static void main (string[] args) throws Exception {String Xmlst r= "<?xml version= ' 1.0 ' encoding= ' UTF-8 ' standalone= ' no '?><root><user><id>1</id><
Name> Zhang San </name><sex> male </sex></user></root> ";
jaxwsdynamicclientfactory DCF = Jaxwsdynamicclientfactory.newinstance ();
Org.apache.cxf.endpoint.Client Client = dcf.createclient ("http://localhost:8080/cxf/user?wsdl");
The URL is the WSDL address of the calling WebService QName name=new QName ("http://service.spring.com/", "GetUser");
Paramvalue is the parameter value object[] Objects=client.invoke (NAME,XMLSTR);
Call the Web service//output to call the result System.out.println ("Output:" +objects[0].tostring ());
/*user user = new User (XMLSTR);
Object obj = User.getuser (); System.out. println ("=========" +obj.tostring ()); */}}
Note here: URL calls to the WebService WSDL address for browser access
http://localhost:8080/cxf/user?wsdl
Path generated, CXF service-side build is successful, need to re-browser access to your configuration path,/user defined in the Spring-cxf.xml configuration file
<jaxws:endpoint id= "Userpoint" implementor= "#user" address= "/user"/>
Effect Diagram:
The URL is the WSDL address of the calling WebService
QName name=new QName ("http://service.spring.com/", "GetUser");
The image is labeled as a namespace, and GetUser refers to the method name defined in the interface.
to here basically even if end, summary of not fine, still hope magnanimous.