WebService Development Note 1--It's so easy to develop webservice with CXF

Source: Internet
Author: User

There is a growing need for SOA concepts in today's projects, and in a recent project I have taken on a business requirement that requires access to a Java-developed web system in a client system developed by. NET, a business requirement that naturally requires the operation of information data through WebService. Below, we will explore in the development of a bit of experience and lessons to summarize the following, for everyone's reference.

The entire architecture of our project uses a more popular WSH MVC combination, namely WEBWORK2 + Spring + Hibernate;
1. First integrate the Apacha CXF WebService into the Spring framework;
Apache Cxf:http://people.apache.org/dist/incubator/cxf/2.0.4-incubator/apache-cxf-2.0.4-incubator.zip
The following CXF configurations are introduced in the spring context configuration file

<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"/>


To add a filter to Web. xml:

<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
Org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>



2. Developing the server-side WebService interface:

/**
* WebService interface Definition class.

* Use @webservice to output all methods in the interface as Web Service.
* You can use annotation to define the method, parameters, and return values in the WSDL.
*/
@WebService
Public interface Webservicesample {


/**
* An easy way to return a string
* @param Hello
* @return
*/
String say (string hello);

/**
* Slightly more complex methods, passing an object to the server processing
* @param user
* @return
*/
String Sayusername (

Userdto user);

/**
* The most complex method to return a list of objects in the Package collection
* @return
*/

@WebResult (partname= "O")
ListObject findusers ();

}


Define three interfaces from simple to complex, simulating business requirements;

3. Implementing the interface

/**
* WebService implementation class.

* Use @webservice to point to the interface definition class.
*/
@WebService (endpointinterface = "Cn.org.coral.biz.examples.webservice.WebServiceSample")
public class Webservicesampleimpl implements Webservicesample {

Public String sayusername (userdto user) {
Return "Hello" +user.getname ();
}

public string say (string hello) {
Return "Hello" +hello;
}

Public ListObject findusers () {
arraylist<object> list = new arraylist<object> ();

List.add (Instancuser (1, "Lib"));
List.add (Instancuser (2, "MLD"));
List.add (Instancuser (3, "LQ"));
List.add (Instancuser (4, "GJ"));
ListObject o = new ListObject ();
O.setlist (list);
return o;
}

Private Userdto Instancuser (Integer id,string name) {
Userdto user = new Userdto ();
User.setid (ID);
User.setname (name);
return user;
}
}



4. Dependent two classes: User objects and List objects

/**
* Web Service is a DTO that transmits user information.

* Isolate the coupling between the entity class and the Web service interface, isolating the effect of the modification of the entity class on the interface.
* Use the annotation callout java-xml mapping with JAXB 2.0, using the default conventions as much as possible.

*/
@XmlAccessorType (Xmlaccesstype.field)
@XmlType (name = "User")
public class Userdto {

protected Integer ID;

protected String name;

Public Integer getId () {
return ID;
}

public void SetId (Integer value) {
id = value;
}

Public String GetName () {
return name;
}

public void SetName (String value) {
name = value;
}
}


With regard to the List object, refer to the description of a problem with JWS: the WebMethod parameter in DK6.0 's webservice does not seem to be ArrayList or other list
Passing the list will need to wrap the list inside other objects (personal understanding if not, please point out), I also encountered such problems in practice. The list object can be passed through the following encapsulated object.

/**
* <p>java class for listObject complex type.
*
* <p>the following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complextype name= "ListObject" >
* <complexContent>
* <restriction base= "{http://www.w3.org/2001/xmlschema}anytype" >
* <sequence>
* <element name= "list" type= "{http://www.w3.org/2001/xmlschema}anytype" maxoccurs= "unbounded" minOccurs= "0"/&gt ;
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType (Xmlaccesstype.field)
@XmlType (name = "ListObject", Proporder = {"List"})
public class ListObject {

@XmlElement (nillable = True)
protected list<object> List;

/**
* Gets The value of the List property.
*
* <p>
* This accessor method returns a reference to the live list,
* Not a snapshot. Therefore any modification do to the
* Returned list would be present inside the JAXB object.
* This is what there is not a <CODE>set</CODE> method for the list property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* GetList (). Add (NewItem);
* </pre>
*
*
* <p>
* Objects of the following type (s) is allowed in the list
* {@link Object}
*
*
*/
Public list<object> getList () {
if (list = = null) {
List = new arraylist<object> ();
}
return this.list;
}

public void setlist (arraylist<object> list) {
This.list = list;
}

}



5.WebService Service-side spring configuration file Ws-context.xml

<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xmlns:jaxws= "Http://cxf.apache.org/jaxws"

Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd "
Default-autowire= "ByName" default-lazy-init= "true" >

<jaxws:endpoint id= "Webservicesample"
address= "/webservicesample" implementor= "Cn.org.coral.biz.examples.webservice.WebServiceSampleImpl"/>

</beans>



WebService Client Spring configuration file Wsclient-context.xml

<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://cxf.apache.org/jaxws
Http://cxf.apache.org/schemas/jaxws.xsd
Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd "
Default-autowire= "ByName" default-lazy-init= "true" >

<!--WS-Client--
<bean id= "identityvalidateserviceclient" class= "Cn.org.coral.admin.service.IdentityValidateService"
Factory-bean= "Identityvalidateserviceclientfactory" factory-method= "create"/>

<bean id= "Identityvalidateserviceclientfactory"
class= "Org.apache.cxf.jaxws.JaxWsProxyFactoryBean" >
<property name= "ServiceClass"
Value= "Cn.org.coral.admin.service.IdentityValidateService"/>
<property name= "Address"
Value= "Http://88.148.29.54:8080/coral/services/IdentityValidateService"/>
</bean>

</beans>


6. After publishing to the Tomcat server, you can view the WSDL generated by the custom WebService interface at the following address:
http://88.148.29.54:8080/aio/services/WebServiceSample?wsdl

7. The JUnit unit test program that calls the WebService interface

package test.coral.sample; 

Import org.springframework.test.AbstractDependencyInjectionSpringContextTests;

Import cn.org.coral.biz.examples.webservice.WebServiceSample;
Import Cn.org.coral.biz.examples.webservice.dto.UserDTO;

public class Testwebservicesample extends
abstractdependencyinjectionspringcontexttests {
Webservicesample webservicesampleclient;

public void setwebservicesampleclient (Webservicesample webservicesampleclient) {
This.webservicesampleclient = webservicesampleclient;
}

@Override
Protected string[] Getconfiglocations () {
Setautowiremode (autowire_by_name);
Spring Client configuration File save location
return new string[] {"Classpath:/cn/org/coral/biz/examples/webservice/wsclient-context.xml " };
}

public void Testwsclinet () {
Assert.hastext ("Webservicesampleclient.say");
}
}

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.