WEBSERVICE with CXF

1. Download CXF and configure environment variables

Configure the bin directory to the path
2. Create a new java project and import the cxf jar package. The final project structure is as follows:

3. ​​Create interface and implement class

1) Interface
package com.tgb.web.webservice; < br>
importjavax.jws.WebService;
@WebService
publicinterfaceHelloWorld{
public String sayHi( String str);
}
2) Implementation class:
/span> package com.tgb.web.webservice.impl;
importjava.util.Date;
importjavax.jws.WebService;
importcom.tgb.web.webservice.HelloWor ld;
@WebService(endpointInterface=“com.tgb.web.webservice. HelloWorld”,serviceName=“HelloWorld”)
publicclassHelloWorldBeanimplementsHelloWorld{
< span style="font-family:Consolas; color:windowtext; font-size:10pt">< span style="color:#646464">@Override
public String sayHi(String str) {
return< /span>“hello,”+str+“, the time now is:”+new Date();

}< /span>
4. Use Endpoint to publish webService, the client is as follows:
package lee;< /span>
import javax.xml.ws.Endpoint;
importcom.tgb.web.webservice.HelloWorld;
< span style="font-family:Consolas; color:#7f055; font-weight:bold; font-size:10pt">importcom.tgb.web .webservice.impl.HelloWorldBean;
publicclassServerMain{ < /div>

public static void main(String[] args) {
HelloWorld hw=new HelloWorldBean();
//Call the publish method of Endpoint to publish Web Service
Endpoint.publish(“http://192.168.1.108:8088/HelloWorld”, hw);
System.out.println(“webService has been released successfully! “);
}

< div> }

5. Visit the address in the browser: http://192.168.1.108:8088/HelloWorld?wsdl, you can see the published wsdl page.

=============== ==================================================
Client:
1. Create a new java project WS_Client, Run cmd and locate the directory Under the src directory under the WS_Client project, and then enter wsdl2java http://192.168.1.108:8088/HelloWorld?wsdl, you can Generate a proxy object that calls wsdl on the client, as shown in the following figure :

2, create a test class ClientMain:
packagelee;

importcom.tgb.web.webservice.impl.HelloWorld; < /div>
publicclass ClientMain {

public< span style="color:#7f055; font-weight:bold">staticvoidmain(String[]args) {

HelloWorld factory=new HelloWorld();

com.tgb.web.webservice.HelloWorld hWorld=factory.getHelloWorldWsPort();

System.out.println(hWorld.sayHi(” Zhang San”));

}

}< /span>

3. Run the test class and it can be called successfully. The final output result is as follows:
hello, Zhang San, the current time is: Wed Mar 23 22:25:24 CST 2016

Put bin The directory is configured in the path

2, Create a new java project and import the cxf jar package. The final project structure is as follows:


3. ​​Establish interface and implement class

1) Interface

package com.tgb.web.webservice;
< /span>

importjavax.jws.WebService;< /p>

@WebService

publicinterfaceHelloWorld{

public String sayHi(String str);

}

2) Implementation class:

< span style="font-family:'Microsoft YaHei UI'; font-size:10.5pt; line-height:1.5"> package com.tgb.web.webservice.impl;

importjava.util.Date;

importjavax.jw s.WebService;

import< span style="color:windowtext; font-weight:normal">com.tgb.web.webservice.HelloWorld;

@WebService(endpointInterface=“com.tgb.web.webservice.HelloWorld” ,serviceName=“HelloWorld”)

publicclassHelloWorldBeanimplementsHelloWorld{

@Override

public String sayHi(String str) {

return“hello,”+str+“,The time now is:”+newDate();

}

}

4. Use Endpoint to publish webService, that is, the client is as follows:

package lee;

importjavax .xml.ws.Endpoint;

importcom.tgb.web.webservice.HelloWorld;

importcom.tgb.web.webservice.impl.HelloWorldBean ;

publicclassServerMain{

publicstaticvoidmain(String[]args){

HelloWorld hw=new HelloWorldBean(); p>

//CallEndpoint’s publish method to publish Web Service

Endpoint.publish(“http://192.168.1.108:8088/HelloWorld”, hw);

System.out.println(“webService successfully released! “);

}

< p>}

5. Visit the address in the browser: http://192.168.1.108: 8088/HelloWorld?wsdl, you can see the published wsdl page.


p>

============================== ==================================

Client:

1. Create a new java project WS_Client, < span style="font-family:'Microsoft YaHei UI'; font-size:10.5pt; line-height:1.5">Run cmd and locate the directory to Under the src directory under the WS_Client project, enter wsdl2java http://192.168.1.108:8088/HelloWo rld?wsdl, you can generate a proxy object that calls wsdl on the client, as shown in the figure below :< /span>


span> < br>

2, create a test class ClientMain :

packagelee;

packagelee ;

import< span style="color:windowtext; font-weight:normal">com.tgb.web.webservice.impl.HelloWorld;

importcom.tgb.web.webservice. impl.HelloWorld;

publicclassClientMain{ < /div>

publicclassClientMain{

publicstaticvoid main(String[]args) {

publicstatic< /span>voidmain(String[]args){

HelloWorld factory=new HelloWorld(); span>

HelloWorld factory=new HelloWorld();

com.tgb.web.webservice.HelloWorld hWorld=factory.getHelloWorldWsPort();

com.tgb.web.webservice.HelloWorld hWorld=factory.getHelloWorldWsPort();

System.out.println(hWorld.sayHi(“张三”));

Sy stem.out.println(hWorld.sayHi(“张三” ));

}

}

}

}

3, run the test class, you can successfully call. The final output is as follows:

hello, Zhang San, the current time is: Wed Mar 23 22:25:24 CST 2016

Leave a Comment

Your email address will not be published.