使用cxf寫web service的簡單實例


實例步驟:

第一步:在myeclipse中新建一個web項目名為webservicetest,並導入依賴的jar包(cxf,spring,apache-commons相關)
commons-logging-1.0.4.jar
cxf-2.6.2.jar
geronimo-jaxws_2.2_spec-1.0.jar
geronimo-ws-metadata_2.0_spec-1.1.3.jar
neethi-3.0.2.jar
cxf結合spring時所需jar包,此例子也需要這些,用到了spring上下文加載:
spring-asm-3.0.7.RELEASE.jar
spring-beans-3.0.7.RELEASE.jar
spring-context-3.0.7.RELEASE.jar
spring-core-3.0.7.RELEASE.jar
spring-expression-3.0.7.RELEASE.jar
spring-aop-3.0.7.RELEASE.jar
spring-web-3.0.7.RELEASE.jar


xmlschema-core-2.0.3.jar
jaxb-api-2.2.6.jar
wsdl4j-1.6.1.jar
jsr173_api-1.0.jar
common-annotations.jar
jaxb-impl-2.0.1.jar
stax-api-1.0.0.jar
wstx-asl-3.2.0.jar
jetty-util-7.5.4.v20111024.jar
jetty-continuation-7.5.4.v20111024.jar
jetty-http-7.5.4.v20111024.jar
jetty-io-7.5.4.v20111024.jar
jetty-security-7.5.4.v20111024.jar
jetty-server-7.5.4.v20111024.jar

@WebService和@WebMethod是WSDL映射Annotation.這些Annotation將描述Web Service的WSDL文檔元素和JAVA源代碼聯系在一起。
@WebService annotation的元素name,serviceName和targetNamespace成員用來描述wsdl:protType,wsdl:service,和targetNameSpace生成WebService中的WSDL文件。
@SOAPBinding是一個綁定的annotation,用來說明網絡協議和格式
@SOAPBinding是一個用來描述SOAP格式和RPC的協議的綁定Annotation.
@WebMethod Annotation的operationName成員描述了wsdl:operation,而且它的操作描述了WSDL文件中的SOAPAction頭部,這是客戶端必須要放入到SOAPHeader中的數值,SOAP1.1中的一種約束。
@WebParam Annotation的partName成員描述了WSDL文檔中的wsdl:part.
@WebResult Annotation的partName成員描述了wsdl:part用來返回WSDL文檔的值。

第二步:創建webservice接口及實現類(下面為Java代碼)
創建HelloWorld接口

View Code

@WebService public interface HelloWorld { @WebMethod String sayHi(@WebParam(name="text")String text); @WebMethod String sayHiToUser(User user); @WebMethod String[] SayHiToUserList(List<User> userList); }

第三步:創建HelloWorldImpl實現類

View Code
@WebService(endpointInterface = "demo.spring.service.HelloWorld", serviceName = "HelloWorld")
//endpointInterface表示該類就必須實現此接口所有方法,serviceName表示webservice的服務名稱
public class HelloWorldImpl implements HelloWorld {
    Map<Integer, User> users = new LinkedHashMap<Integer, User>();
    @WebMethod
    public String sayHi(String text) {
        return "Hello " + text;
    }
    @WebMethod
    public String sayHiToUser(User user) {
        users.put(users.size() + 1, user);
        return "Hello " + user.getName();
    }
    @WebMethod
    public String[] SayHiToUserList(List<User> userList) {
        String[] result = new String[userList.size()];
        int i = 0;
        for (User u : userList) {
            result[i] = "Hello " + u.getName();
            i++;
        }
        return result;
    }
}

第四步:啟動webserviceApp.java,訪問http://localhost:8080/helloWorld?WSDL

View Code

public class WebServiceApp { public static void main(String[] args) { System.out.println("web service start"); HelloWorldImpl implementor= new HelloWorldImpl(); String address="http://localhost:8080/helloWorld"; Endpoint.publish(address, implementor); System.out.println("web service started"); } }

第五步:在web.xml中加入cxf相應配置,內容如下:

View Code

<!--cxf start-->  
<!--用於加載applicationContext.xml配置信息-->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>WEB-INF/classes/applicationContext.xml</param-value>  
    </context-param>  
    <!--使用spring ContextLoaderListener 加載applicationContext.xml-->  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
<!--配置CXFServlet-->  
    <servlet>  
        <servlet-name>CXFServlet</servlet-name>  
        <display-name>CXF Servlet</display-name>  
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>CXFServlet</servlet-name>  
        <!-- url可自定義配置,用於CXFServlet請求地址攔截,訪問會用到 -->  
        <url-pattern>/webservice/*</url-pattern>  
    </servlet-mapping>  
<!--cxf end -->  

第六步:在src中創建基本的applicationContext.xml內容如下(作用:主要做webservice接口屬性配置,通過web.xml配置加載)

View Code

<?xml version="1.0" encoding="UTF-8"?>
<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" />

<!--id:名稱(隨意配),implementor:指定接口具體實現類,address:隨意配-->  
    <jaxws:endpoint id="helloWorld" implementor="demo.spring.service.HelloWorldImpl" address="/HelloWorld" /> 

<!-- WebService 客戶端 spring 配置文件 cxf與Spring集成,cxf里提供了一個工廠類org.apache.cxf.jaxws.JaxWsProxyFactoryBean,可以方便實現的調用WebService。 serviceClass屬性是接口類,address是webService的路徑 在其他bean里如果要調用webservice,只要將client這個bean注入到需要使用的bean里。 -->
    <bean id="client" class="demo.spring.service.HelloWorld" factory-bean="clientFactory" factory-method="create" />

    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="demo.spring.service.HelloWorld" />
        <property name="address" value="http://localhost:8080/webservices/HelloWorld" />
    </bean>
</beans>

第七步:發布webservice到tomcat

Java代碼,驗證WSDL是否發布成功:
如果項目發布放在/webapps/ROOT下時:
訪問http://IP地址:端口/webservices/{applicationContent.xml中配置的address}?wsdl
//webservices對應web.xml中的<url-pattern>/webservices/*</url-pattern>驗證是否能正常看到xml格式的頁面

第八步:測試

View Code

public class HelloWorldClient { public static void main(String[] args) { /** * 簡單的用ApplicationContext做測試的話,獲得Spring中定義的Bean實例(對象),可以用以下方法 * ClassPathXmlApplicationContext[只能讀放在web-info/classes目錄下的配置文件] */ ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" }); HelloWorld client = (HelloWorld) context.getBean("client"); User user = new User(); user.setName("Tony"); user.setDescription("test"); String str = client.sayHiToUser(user); System.out.println(str); } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM