在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); } }