1. 程式人生 > 實用技巧 >java--->wsdl的簡單使用(spring+cxf)

java--->wsdl的簡單使用(spring+cxf)

1.wsdl的配置

applicationContext.xml

<?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:cxf="http://cxf.apache.org/core"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd 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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
> <!--匯入與CXF框架有關的xml --> <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" /> <bean id="testService" class="com.test.impl.TestWsdlImpl"
> </bean> <jaxws:endpoint id="testServicePort" implementor="#testService" address="/testService"> </jaxws:endpoint> </beans>
applicationContext.xml

2.簡單例子

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface TestWsdl {
    @WebMethod(operationName = "getValue")
    String getValue(@WebParam(name="condition")String condition);
}
TestWsdl
import javax.jws.WebService;

import com.test.TestWsdl;

@WebService(endpointInterface = "com.test.TestWsdl", serviceName = "testService")
public class TestWsdlImpl implements TestWsdl{

    public String getValue(String condition) {
        
        return "my method getValue para is "+condition;
    }

}
TestWsdlImpl

3.呼叫簡單例項

axis-1.4.jar
axis-jaxrpc-1.4.jar
commons-codec-1.11.jar
commons-dbcp.jar
commons-discovery-0.5.jar
commons-httpclient-3.1.jar
commons-logging-1.1.1.jar
commons-logging-api-1.1.jar
commons-pool.jar
wsdl4j-1.6.2.wso2v4.jar

import org.apache.axis.client.Call;   
import org.apache.axis.client.Service;   


public class ServiceTest {

    public static void main(String[] args) throws Exception {
        try {
            String endpoint = "http://localhost:8080/sfPrintService/sfws/testService?wsdl";
            // 直接引用遠端的wsdl檔案
            // 以下都是套路
            Service service = new Service();
            Call call = (Call) service.createCall();
            //call.setTargetEndpointAddress(new URL(endpoint));
            call.setTargetEndpointAddress(endpoint);
            call.setOperationName("getValue");// WSDL裡面描述的介面名稱
            call.addParameter("condition",
                    org.apache.axis.encoding.XMLType.XSD_STRING,//XSD_DATE
                    javax.xml.rpc.ParameterMode.IN);// 介面的引數
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 設定返回型別
            String temp = "測試人員";
            Object[] c = new Object[] { temp };
            String result = (String) call.invoke(c);
            //String result1 = (String) call.invoke(arg0)
            // 給方法傳遞引數,並且呼叫方法
            System.out.println("result is " + result);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
}
ServiceTest