spring4.2.0 + cxf 3.1.8 webService

学习webService笔记整理

spring4.2.0 + cxf 3.1.8 实现webService

 cxf 需要的jar

spring4.2.0 + cxf 3.1.8 webService_第1张图片

web.xml中必须要加入spring的配置,只加入springMVC的配置会失败

web.xml完整配置


    contextConfigLocation
    classpath:applicationContext.xml


    org.springframework.web.context.ContextLoaderListener



    springDispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
        contextConfigLocation    
        classpath:springmvc.xml
    
    1



    springDispatcherServlet
    /



    CXFService
    org.apache.cxf.transport.servlet.CXFServlet


    CXFService
    /webservice/*

接口:

@WebService
    public interface HelloWebService {
    public int add(@WebParam(name = "num1") int num1, @WebParam(name = "num2") int num2);
}

实现类:

@WebService(endpointInterface = "com.webservice.HelloWebService" , serviceName = "HelloWebService")
public class HelloWebServiceImpl implements HelloWebService {
    @Override
    public int add(int num1, int num2) {
        return num1 + num2;
    }
}

spring配置文件:

启动服务测试:

spring4.2.0 + cxf 3.1.8 webService_第2张图片

代码调用:

String wsdlUrl = "http://10.216.70.95:8088/TestUeditor/webservice/helloWorld?wsdl";
JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
Client createClient = clientFactory.createClient(wsdlUrl);
QName operationName = WsUtil.getOperationName(createClient.getEndpoint(), "add");
Object[] invoke = createClient.invoke(operationName, 1 , 7);
System.out.println(invoke[0]);

WsUtil类:

public class WsUtil {
    public static QName getOperationName(Endpoint endpoint, String operation) {
        QName opName = new QName(endpoint.getService().getName().getNamespaceURI(), operation);
        BindingInfo bindingInfo = endpoint.getEndpointInfo().getBinding();
        if (bindingInfo.getOperation(opName) == null) {
            for (BindingOperationInfo operationInfo : bindingInfo.getOperations()) {
                if (operation.equals(operationInfo.getName().getLocalPart())) {
                    opName = operationInfo.getName();
                    break;
                }
            }
        }
        return opName;
    }
}



你可能感兴趣的:(java)