1、引入pom依賴
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.3.3</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.2.2</version> </dependency>
2、編寫webservice服務端
2.1 新建接口,並添加targetNamespace,targetNamespace是接口所在包的倒序,如果不添加,在動態調用接口的時候,會報錯誤信息。
import javax.jws.WebParam; import javax.jws.WebService; // ~ File Information /** * @author yinyl * @date 2020年12月27日 下午2:45:10 * 類說明:定義接口,targetNamespace是當前所在包的倒序。 */ @WebService(targetNamespace="http://webservice.common.bmSystem.com") public interface IHelloWord { // ~ Methods public String say(@WebParam(name="helloName") String helloName); }
2.2 新建接口實現類,targetNamespace與實現接口保持一致,endpointInterface為接口所在包的全路徑
import javax.jws.WebService; import org.springframework.stereotype.Component; import com.bmSystem.common.webservice.IHelloWord; // ~ File Information /** * @author zxy * @date 2019年9月23日 下午2:46:18 * 類說明:接口實現類,targetNamespace是當前所在包的倒序。 */ @Component @WebService(targetNamespace="http://webservice.common.bmSystem.com", endpointInterface ="com.bmSystem.common.webservice.IHelloWord") public class HelloWordImpl implements IHelloWord{ @Override public String say(String str) { System.out.println("進入接口..."); return str; } }
3、發布webservice接口
package com.bmSystem.common.sys.config.webservice; import javax.xml.ws.Endpoint; import org.apache.cxf.Bus; import org.apache.cxf.bus.spring.SpringBus; import org.apache.cxf.jaxws.EndpointImpl; import org.apache.cxf.transport.servlet.CXFServlet; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.bmSystem.common.webservice.impl.HelloWordImpl; // ~ File Information /** * @author zxy * @date 2019年9月23日 下午3:12:00 * 類說明:webservice發布,默認訪問地址為:localhost:8080/services/helloWord?wsdl * */ @Configuration public class WebServiceConfig { // ~ Fields @Autowired private HelloWordImpl helloWord;//接口實現類 // ~ Methods /** * 此方法作用是改變項目中服務名的前綴名,此處127.0.0.1或者localhost不能訪問時,請使用ipconfig查看本機ip來訪問 * 此方法被注釋后:wsdl訪問地址為http://127.0.0.1:8080/services/user?wsdl * 去掉注釋后:wsdl訪問地址為:http://127.0.0.1:8080/soap/user?wsdl * @return */ /* * @SuppressWarnings("all") * * @Bean public ServletRegistrationBean dispatcherServlet() { return new * ServletRegistrationBean(new CXFServlet(), "/soap/*"); } */ @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public Endpoint endpoint() { EndpointImpl endpoint=new EndpointImpl(springBus(), helloWord); endpoint.publish("/helloWord");//訪問地址 return endpoint; } }
以上內容在實際運用中,已實現~
4、開發webservice客戶端調用,這里采用的是動態調用方式(推薦)
import java.util.HashMap; import java.util.Map; import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import com.google.gson.Gson; /** * @author zxy * @date 2020年6月5日 上午9:05:06 * @Description : 動態調用webservice接口 */ public class TestWebservice { public static void main(String[] args) { // 創建動態客戶端 JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient("http://127.0.0.1:8085/services/helloWord?wsdl"); // 需要密碼的情況需要加上用戶名和密碼 // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD)); Object[] objects = new Object[0]; try { // invoke("方法名",參數1,參數2,參數3....); //json的形式 Map<String,Object> params=new HashMap<String,Object>(); params.put("name", "dasda"); Gson gson = new Gson(); String json = gson.toJson(params); objects = client.invoke("say", json); System.out.println("返回數據:" + objects[0]); } catch (java.lang.Exception e) { e.printStackTrace(); } } }
axis方式調用webservice接口:
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 Test01 { public static void main(String[] args) { try { //1、直接引用遠程的wsdl文件 String endpoint = "接口訪問路徑"; Service service = new Service(); Call call = (Call) service.createCall(); //創建服務 call.setTargetEndpointAddress(endpoint); //2、定義報名和接口方法 call.setOperationName(new QName("targetNamespace", //wsdl文件中的targetNamespace "getAllResourceDetail") //接口實現功能的方法 ); //3、設置參數 call.addParameter("resType", XMLType.XSD_INT,ParameterMode.IN);// 接口的參數 call.addParameter("nodeIndexCode",XMLType.XSD_STRING,ParameterMode.IN);// 接口的參數 call.setReturnType(XMLType.XSD_STRING);// 設置返回類型 int resType=1000; String nodeIndexCode=""; //4、給方法傳遞參數,並且調用方法 String result = (String) call.invoke(new Object[] {nodeIndexCode ,resType}); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } }
