Create a Web Service with Apache CXF and Jboss 6 on MyEclipse


参考:http://www.celinio.net/techblog/?p=531
 
      I have recentlystartrted studying Apache CXF、the open source weservice ffraameework. I amfamimimimimirir with developing Web Services using EJB 3、Axis or Glue.But not with CXF.Unitil now.Unitil now.ccccxxxxxxxxxxFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFJAX-S and JAX-R PC speciiiiications.Developing Web Services using CXF and JBoss is quite eaeay.The only annoying papass is to figureeeuaah JARs libriririesto inuclclaspath and whihihihihihihihihihihihihihishshshshshshshshshshshandand and wwwwwhihihihihishshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshandand and and and wwwwwwwww:BMI=weight/(height x height)
 
1、First create a Web Project in MyEclipse 8.6.
 
2、Download the CXF frame ebook apache-cxf-23.11.zipat http://cxf.apache.org/download.html
 
3、After unzipping the archive、I added to WEB-INF/lib all the libries that are inside the apphe-cxf-23.1\lib 
folder.It is of course not a good habit to have since adding all sorts of libries can produces conflicts.I had to remove a few jars such as:jaxb-xjc-2.5.1.jar,xalan-2.7.1.jar.serize.jar.
 
 
 
Create a Web Service with Apache CXF and Jboss6 on MyEclipse_第1张图片
 
The re are probably a few extras jars that are not need but at least they do produce any error for that simple web service. 
 
4、Create the Service Endpoint Interface(SEI).Here I use the bottom-up approach(code first):that is first create a Java clast will be converted into a web service.The other ap proach Torson. 
 
package com.company.bmi.services;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface IBMICalculator {
    public  double computeBMI(@WebParam(name="weight") double weight, @WebParam(name="height") double height) ;
}
 
 
5、Create the class that implemens this interface.It will implement the operation s defined by the service: 
 
package com.company.bmi.services;

public class IBMICalculatorImpl implements IBMICalculator{
    @Override
    public double computeBMI(double weight, double height) {
        return weight / (height * height);
    }
 
 6、Add the Spring-based configration file、cxf.xml、under the package directory、for instance:
 
<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.springframework.org/schema/beans
                          http://www.springframework.org/schema/beans/spring-beans.xsd
                          http://cxf.apache.org/jaxws
                          http://cxf.apache.org/schemas/jaxws.xsd">

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

  <jaxws:endpoint id="calcBMI"
                  implementor="com.company.bmi.services.IBMICalculatorImpl"
                  address="/cxfBmi"/>
</beans>
 
 
7、You need to udate the deployment descriptor file WEB-INF/web.xml: 
 
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>BMI</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:com/company/bmi/services/cxf.xml</param-value>
  </context-param>

  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>

  <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>
</web-app>
 
 
I specify the path where to find the cxf.xml file and also the CXFSS ervlet. 
7、Finally create a client that uss this web service.This a standrone Java clast that invokes the IBMIlculator web service: 
 
package com.company.bmi.client;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.company.bmi.services.IBMICalculator;

public final class Client {
    private Client() {
    }

    public static void main(String args[]) throws Exception {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

        factory.getInInterceptors().add(new LoggingInInterceptor());
        factory.getOutInterceptors().add(new LoggingOutInterceptor());
        factory.setServiceClass(IBMICalculator.class);
        factory.setAddress("http://localhost:8085/BMI/services/cxfBmi");
        IBMICalculator client = (IBMICalculator) factory.create();
        Double bmi = client.computeBMI(75, 170);
        System.out.println("BMI : " + bmi);
    }
}
 
 
9、run BMI on Jboss As 6
Create a Web Service with Apache CXF and Jboss6 on MyEclipse_第2张图片
The available SOAP services are listed the follwing URL: 
 
http://localhost:8085/BMI/services/

 
10、WSDL: 
http://localhost:8085/soa_apachecxf_bmi/services/cxfBmi?wdl 
 
 
<wsdl:definitions name="IBMICalculatorImplService" targetNamespace="http://services.bmi.company.com/">
<wsdl:types>
<xs:schema elementFormDefault="unqualified" targetNamespace="http://services.bmi.company.com/" version="1.0">
<xs:element name="computeBMI" type="tns:computeBMI"/>
<xs:element name="computeBMIResponse" type="tns:computeBMIResponse"/>
<xs:complexType name="computeBMI">
<xs:sequence>
<xs:element name="weight" type="xs:double"/>
<xs:element name="height" type="xs:double"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="computeBMIResponse">
<xs:sequence>
<xs:element name="return" type="xs:double"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="computeBMIResponse">
<wsdl:part element="tns:computeBMIResponse" name="parameters">
    </wsdl:part>
</wsdl:message>
<wsdl:message name="computeBMI">
<wsdl:part element="tns:computeBMI" name="parameters">
    </wsdl:part>
</wsdl:message>
<wsdl:portType name="IBMICalculator">
<wsdl:operation name="computeBMI">
<wsdl:input message="tns:computeBMI" name="computeBMI">
    </wsdl:input>
<wsdl:output message="tns:computeBMIResponse" name="computeBMIResponse">
    </wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="IBMICalculatorImplServiceSoapBinding" type="tns:IBMICalculator">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="computeBMI">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="computeBMI">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="computeBMIResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="IBMICalculatorImplService">
<wsdl:port binding="tns:IBMICalculatorImplServiceSoapBinding" name="IBMICalculatorImplPort">
<soap:address location="http://localhost:8085/BMI/services/cxfBmi"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
 
The e e e e e e is one operation:computteBMI.The input is comput is compute ponse
11、Theレスポンス、displayed when running the client(java code):
 
---------------------------
ID: 1
Address: http://localhost:8085/BMI/services/cxfBmi
Encoding: UTF-8
Content-Type: text/xml
Headers: {SOAPAction=[""], Accept=[*/*]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:computeBMI xmlns:ns1="http://services.bmi.company.com/"><weight>75.0</weight><height>170.0</height></ns1:computeBMI></soap:Body></soap:Envelope>
--------------------------------------
30 janv. 2011 17:56:14 org.apache.cxf.interceptor.AbstractLoggingInterceptor log
INFO: Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {content-type=[text language="/xml;charset=UTF-8"][/text][/text], Date=[Sun, 30 Jan 2011 16:56:14 GMT], Content-Length=[241], X-Powered-By=[Servlet/3.0; JBossAS-6], Server=[Apache-Coyote/1.1]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:computeBMIResponse xmlns:ns2="http://services.bmi.company.com/"><return>0.0025951557093425604</return></ns2:computeBMIResponse></soap:Body></soap:Envelope>
--------------------------------------
BMI : 0.0025951557093425604