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
原文地址:https://www.cnblogs.com/cai170221/p/13640078.html