在springmvc的配置文件添加创建如下的bean:
<!-- 暴露一个webService连接 --> <bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter"> <property name="baseAddress" value="http://localhost:8085/"/> </bean>
创建一个接口和实现类
package com.nbesoft.company.service; public interface ReceiveDataService { public String exporeInterfaceService(String data); }
package com.nbesoft.company.service.impl; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import org.springframework.stereotype.Service; import com.nbesoft.company.service.ReceiveDataService; @Service @WebService(name="dataService",serviceName="receiveDataServiceImpl",targetNamespace="dataReceive") public class ReceiveDataServiceImpl implements ReceiveDataService { @WebMethod public String exporeInterfaceService(@WebParam(name="data") String data) { System.out.println("=========="); System.out.println("data:"+data); System.out.println("=====---------====="); return "外部传送数据进来了..."+data; } }
调用接口
需要如下jar包
调用的相关代码:
package ss; import java.net.URL; import java.rmi.RemoteException; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; public class Testss { public static void main(String[] args) { // 访问的url String url = "http://localhost:8085/receiveDataServiceImpl?wsdl"; // 命名空间 String namespace_uri = "dataReceive"; // 暴露的方法名 String methodName = "exporeInterfaceService"; // 创建Service实例 Service service = new Service(); Call call = null; try { // 通过Service创建Call的实例 call = (Call) service.createCall(); // 将对应的WSDL的URL路径加入call的实例 call.setTargetEndpointAddress(new URL(url)); } catch (Exception e) { e.printStackTrace(); } // 设置要访问的方法 call.setOperationName(new QName(namespace_uri,methodName)); // 设置要访问的方法里面需要传入的参数 call.addParameter(new QName("data"), XMLType.XSD_STRING, ParameterMode.IN); // 设置返回参数 call.setReturnType(XMLType.XSD_STRING, String.class); String str = "哈哈哈"; Object[] obj = {str}; Object result = null; try { // 调用 result = call.invoke(obj); } catch (RemoteException e) { e.printStackTrace(); } // 打印返回结果 System.out.println("*****"+result); } }