Use Maven to implement CXF.
1, Development server
Look at POM first
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0modelVersion> <groupId>org.qfgroupId> <artifactId>M_CXF_Server1artifactId> <version>0.0 .1-SNAPSHOTversion> <description> Web Service based on Apache's CXF implementationdescription span>> <dependencies> <dependency > <groupId>org.apache.cxfgroupId> <artifactId> cxf-rt-transports-http-jettyartifactId> <version>3.1.12version > dependency> dependencies> <build> <plugins> <plugin> <groupId span>>org.apache.maven.pluginsgroupId> <artifactId>maven-compiler-pluginartifactId> <version>3.6.0version> <configuration> < source>1.8source> <target>1.8target span>> <encoding>UTF-8encoding> configuration > plugin> < span class="hljs-title">plugins> build>project>
Code:
Interface:
@WebService//Mark this as a Web servicepublic interface FoodInterface { String getF(); String getFByN(@WebParam(name="name")String n);}
Interface implementation class:
public class FoodInterfaceImple implements FoodInterface{ @Override public String < span class="hljs-title">getF() {// TODO Auto-generated method stub System.out.println( "Server: Get the request "); return "Takeaway: Rice";} @Override public String getFByN(String n) {// TODO Auto-generated method stub System.out.println("Server: --"+n+"---Get request"); return n+", eat fried noodles at noon "; }}
Post code:
public static void main(String[] args) {//1. Create a cxf service factory object JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean(); //2, set the interface class to be published factory Bean.setServiceClass(FoodInterface.class); //3, set the implementation class object of the interface factoryBean.setServiceBean(new span> FoodInterfaceImple()); //4, set publishing path, access address factoryBean.setAddress("http:// 127.0.0.1:9797/ws/food"); //5, release factoryBean.create(); System.out.println("Service Start..."); //wsdl2java -p org. qf.ws -d G:/src -client http://127.0.0.1:9797/ws/food?wsdl //Verification is successful: http:/ /127.0.0.1:9797/ws/food?wsdl }
2, use CXF’s own tool to automatically generate client code Code
Configure the decompressed bin of cxf to the path and use the command to generate it in xmd:
wsdl2java -p org.qf.ws -d G:/src -client http://127.0 .0.1:9797/ws/food?wsdl
Then go to src under G drive and copy the generated code to the client project.
Parameter description:
-p is the package corresponding to the package in Java
-d Enter the directory, the generated .java file will be in this directory, and the package path configured by the -p parameter will be automatically added.
– client generates the code for testing the web service on the client side.
-server generates the code for the server to start the web service.
-impl generates the code for the implementation of the web service.
-ant generates the build.xml file.
– all Generate all files corresponding to the above -client -server -impl -ant.
Note that if an error is automatically generated, please refer to: CXF automatically generates client code exception jdk1.8
3.Develop the client code
The pom file is exactly the same as the server
Copy the automatically generated code to the src
Client’s code:
public static void main(String[] args) throws MalformedURLException {test3();} //Use CXF Subclass of automatically generated Service---recommended public static void test1(){ //Create an automatically generated Servcie class object FoodInterfaceService factory=new FoodInterfaceService(); //Get the implementation class of the interface--server FoodInterface fi=factory.getFoodInterfacePort(); System.out.println("Client:"+fi.getF()); System.out.println("Client: 1---"+fi.getFByN("Grasshopper"));} //Use CXF built-in object call public < span class="hljs-keyword">static void test2(){ //Create a proxy factory object JaxWsProxyFactoryBean factoryBean=new JaxWsProxyFactoryBean(); / /Set the Class object of the interface factoryBean.setServiceClass(FoodInterface.class); //Set the service address factoryBean.setAddress("http://127.0.0.1:9797/ws/food?wsdl"); //Get the implementation class of the interface span> FoodInterface fi=(FoodInterface) factoryBean.create(); System.out.println("Client: 44- -->"+fi.getF());} //Use Service to call service public span> static void test3() throws MalformedURLException{ //Create wsdl path URL url=new URL("http://127.0.0.1:9797/ws/food?wsdl"); QName qn=new QName("http://ws.code404.cn/", "FoodInterfaceService"); Service service=Service.create(url, qn ); FoodInterface fi=service.getPort(Food Interface.class); System.out.println("Client: ooo---->" +fi.getFByN("frog")); }
Just run it.