WebService CXF Use

Source: Internet
Author: User

Environment: jdk1.7 grails2.4.0 Ubuntu13

Required JAR Package: cxf-2.0.1

Server-side:

Define the interface class:

public interface iapp{

....//interface method.

}

To define an implementation class:

public class Appservice implements iapp{

Defines which protocol to use for remote invocation methods; RMI can not cross firewalls;

static expose [' CXF ', ' rmi '];

Remove the method that does not need to be called by the client;

static excludes = [method name];

....//method to achieve

}

After the project is started, enter http://project name/services/app?wsdl on the browser, and the entire page will generate an XML file stating the success of the CXF release;

The client is generated after the server publishing is complete:

Eclipse:

1. Create a project first;

2. Right-click the new-->other-->web Service client--and next, enter the ip:port/project name/SERVICES/APP?WSDL, which generates the client;

3. Configure the Wsdl2java Environment, window command line output:

The-p:wsdl namespace, the so-called package name; -D: Specifies the path where the code is to be generated;

E:\code\aa\code>wsdl2java-p com.boful.nts.wsdl.client-d. -all http://192.168.1
.117:8080/nts/services/app?wsdl production of jar packages using the Eclipse Export tool after production is complete; 4. Place the generated jar package in the server project; 5. To execute the off-hours method when a remote call is required: Appservice Appservice = new Appservice (the new URL (HTTP/Accessip:port/project name/services/app?wsdl)); Appserviceporttype PortType = Appservice.getappserviceport ();p Orttype call the remote method;//The method called is generated by the client itself, Therefore, when the object is added and deleted and the server-side interface objects in the package is different, you need to make two of objects to each other to transfer the value; simple two classes convert code to and from one another:

Package com.boful.nts;

Import Java.lang.reflect.Field;
Import Java.lang.reflect.Method;
Import Java.lang.reflect.Modifier;
Import Java.util.Arrays;

public class Bofulclassutils {

public static void Cloneobject (Object src, Object dest, string[] ignored) {
class<?> Class1 = Src.getclass ();
class<?> Class2 = Dest.getclass ();
field[] fields1 = Class1.getdeclaredfields ();
field[] Fields2 = Class2.getdeclaredfields ();
method[] methods1 = Class1.getdeclaredmethods ();
method[] Methods2 = Class2.getdeclaredmethods ();
System.out.println (fields1.length);
for (Field field:fields1) {

String fieldName1 = Field.getname ();
if (ignored! = null && ignored.length > 0) {
if (Arrays.binarysearch (ignored, fieldName1) < 0) {
Continue
}
}


Boolean iscontain = false;
for (Field field2:fields2) {
if (Field2.getname (). Equals (fieldName1)) {
Iscontain = true;
Break
}
}

if (Iscontain) {
Iscontain = false;
String setmethodname = "Set"
+ fieldname1.substring (0, 1). toUpperCase ()
+ fieldname1.substring (1);
String GetMethod = "get";
if ((Field.gettype () ==boolean.class) | | (Field.gettype () ==boolean.class)) {
GetMethod = "is";
}
String getmethodname = getmethod + fieldname1.substring (0, 1). toUpperCase ()
+ fieldname1.substring (1);

System.out.println (Setmethodname);
for (Method method:methods2) {

if (Method.getname (). Equals (Setmethodname)) {
if (method.getreturntype () = = Void.class) {
if (method.getmodifiers () = = Modifier.public) {
Class<?>[] Paramtypes = method
. Getparametertypes ();
System.out.println ("--------------------" + field.getdeclaringclass (). GetName ());
if ((paramtypes.length = = 1)
&& (paramtypes[0] = = Field.gettype ())) {
try {
Method Getmehod = Class1.getmethod (getmethodname);
Object value = Getmehod.invoke (SRC);
Method method2 = Class2
. GetMethod (Setmethodname,
Field.gettype ());
Method2.invoke (dest, value);
} catch (Exception e) {
E.printstacktrace ();
}
}
}
}
}
}
}
}
}

public static void Cloneobject (Object src, object dest) {
Cloneobject (SRC, dest, null);
}
}

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.