1、gradle引入依賴的增強第三方包
implementation 'org.apache.cxf:cxf-spring-boot-starter-jaxws:3.2.6'
2、增強類方法
package webservice; import com.alibaba.fastjson.JSON; import com.landray.kmss.sys.notify.webservice.Exception_Exception; import com.landray.kmss.sys.notify.webservice.ISysNotifyTodoWebService; import com.landray.kmss.sys.notify.webservice.NotifyTodoAppResult; import com.landray.kmss.sys.notify.webservice.NotifyTodoSendContext; import cn.togeek.oa.OAConstant; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.cxf.endpoint.Client; import org.apache.cxf.frontend.ClientProxy; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.apache.cxf.message.Message; public class WsdlHelper { public static <T> T callService(String address, Class<T> serviceClass, Map<String, List<String>> headers) { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); // 記錄入站訊息 factory.getInInterceptors().add(new LoggingInInterceptor()); // // 記錄出站訊息 factory.getOutInterceptors().add(new LoggingOutInterceptor()); // 新增訊息頭驗證資訊。如果服務端要求驗證使用者密碼,請加入此段程式碼 factory.setServiceClass(serviceClass); factory.setAddress(address); T t = (T) factory.create(); Client proxy = ClientProxy.getClient(t); // 建立 HTTP headers // Map<String, List<String>> headers = new HashMap<>(); // headers.put("appKey", Collections.singletonList("657963db227be6104fa35a8c")); // 新增 HTTP headers 到 webservice 請求中 proxy.getRequestContext().put(Message.PROTOCOL_HEADERS, headers); // 使用MTOM編碼處理訊息。如果需要在訊息中傳輸文件附件等二進位制內容,請加入此段程式碼 // Map props = new HashMap(); // props.put("mtom-enabled", Boolean.TRUE); // factory.setProperties(props); // 建立服務代理並返回 return t; } public static void main(String[] args) throws Exception_Exception { Map<String, List<String>> headers = new HashMap<>(); String appKey = "657963db227be6104fa35a8c"; headers.put("appKey", Collections.singletonList(appKey)); ISysNotifyTodoWebService service = callService("http://test.com/sys/webservice/sysNotifyTodoWebService?wsdl", ISysNotifyTodoWebService.class, headers); NotifyTodoAppResult todoCount = service.getTodoCount(null); System.out.println(JSON.toJSONString(todoCount)); NotifyTodoSendContext baseEntity = OAConstant.getBaseEntity(new NotifyTodoSendContext()); baseEntity.setModelId("123"); baseEntity.setLink("www.baidu.com"); baseEntity.setSubject("測試一下下···"); baseEntity.setType(OAConstant.TODOSYNC_TYPE); NotifyTodoAppResult todo = service.sendTodo(appKey, baseEntity); System.out.println(JSON.toJSONString(todo)); } }
3、webservice客戶端定義
package com.landray.kmss.sys.notify.webservice; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.xml.bind.annotation.XmlSeeAlso; import javax.xml.ws.RequestWrapper; import javax.xml.ws.ResponseWrapper; /** * This class was generated by the JAX-WS RI. * JAX-WS RI 2.2.9-b130926.1035 * Generated source version: 2.2 * */ @WebService(name = "ISysNotifyTodoWebService", targetNamespace = "http://webservice.notify.sys.kmss.landray.com/") @XmlSeeAlso({ ObjectFactory.class }) public interface ISysNotifyTodoWebService { /** * * @param arg0 * @return * returns com.landray.kmss.sys.notify.webservice.NotifyTodoAppResult * @throws Exception_Exception */ @WebMethod @WebResult(targetNamespace = "") @RequestWrapper(localName = "updateTodo", targetNamespace = "http://webservice.notify.sys.kmss.landray.com/", className = "com.landray.kmss.sys.notify.webservice.UpdateTodo") @ResponseWrapper(localName = "updateTodoResponse", targetNamespace = "http://webservice.notify.sys.kmss.landray.com/", className = "com.landray.kmss.sys.notify.webservice.UpdateTodoResponse") public NotifyTodoAppResult updateTodo( @WebParam(name = "arg0", targetNamespace = "") NotifyTodoUpdateContext arg0) throws Exception_Exception ; /** * * @param arg0 * @return * returns com.landray.kmss.sys.notify.webservice.NotifyTodoAppResult * @throws Exception_Exception */ @WebMethod @WebResult(targetNamespace = "") @RequestWrapper(localName = "setTodoDone", targetNamespace = "http://webservice.notify.sys.kmss.landray.com/", className = "com.landray.kmss.sys.notify.webservice.SetTodoDone") @ResponseWrapper(localName = "setTodoDoneResponse", targetNamespace = "http://webservice.notify.sys.kmss.landray.com/", className = "com.landray.kmss.sys.notify.webservice.SetTodoDoneResponse") public NotifyTodoAppResult setTodoDone( @WebParam(header = true, name = "appkey") String appkey, @WebParam(name = "arg0", targetNamespace = "") NotifyTodoRemoveContext arg0) throws Exception_Exception ; /** * * @param arg0 * @return * returns com.landray.kmss.sys.notify.webservice.NotifyTodoAppResult * @throws Exception_Exception */ @WebMethod @WebResult(targetNamespace = "") @RequestWrapper(localName = "getTodo", targetNamespace = "http://webservice.notify.sys.kmss.landray.com/", className = "com.landray.kmss.sys.notify.webservice.GetTodo") @ResponseWrapper(localName = "getTodoResponse", targetNamespace = "http://webservice.notify.sys.kmss.landray.com/", className = "com.landray.kmss.sys.notify.webservice.GetTodoResponse") public NotifyTodoAppResult getTodo( @WebParam(name = "arg0", targetNamespace = "") NotifyTodoGetContext arg0) throws Exception_Exception ; /** * * @param arg0 * @return * returns com.landray.kmss.sys.notify.webservice.NotifyTodoAppResult * @throws Exception_Exception */ @WebMethod @WebResult(targetNamespace = "") @RequestWrapper(localName = "getTodoCount", targetNamespace = "http://webservice.notify.sys.kmss.landray.com/", className = "com.landray.kmss.sys.notify.webservice.GetTodoCount") @ResponseWrapper(localName = "getTodoCountResponse", targetNamespace = "http://webservice.notify.sys.kmss.landray.com/", className = "com.landray.kmss.sys.notify.webservice.GetTodoCountResponse") public NotifyTodoAppResult getTodoCount( @WebParam(name = "arg0", targetNamespace = "") NotifyTodoGetCountContext arg0) throws Exception_Exception ; /** * * @param arg0 * @return * returns com.landray.kmss.sys.notify.webservice.NotifyTodoAppResult * @throws Exception_Exception */ @WebMethod @WebResult(targetNamespace = "") @RequestWrapper(localName = "deleteTodo", targetNamespace = "http://webservice.notify.sys.kmss.landray.com/", className = "com.landray.kmss.sys.notify.webservice.DeleteTodo") @ResponseWrapper(localName = "deleteTodoResponse", targetNamespace = "http://webservice.notify.sys.kmss.landray.com/", className = "com.landray.kmss.sys.notify.webservice.DeleteTodoResponse") public NotifyTodoAppResult deleteTodo( @WebParam(header = true, name = "appkey") String appkey, @WebParam(name = "arg0", targetNamespace = "") NotifyTodoRemoveContext arg0) throws Exception_Exception ; /** * * @param arg0 * @return * returns com.landray.kmss.sys.notify.webservice.NotifyTodoAppResult * @throws Exception_Exception */ @WebMethod @WebResult(targetNamespace = "") @RequestWrapper(localName = "sendTodo", targetNamespace = "http://webservice.notify.sys.kmss.landray.com/", className = "com.landray.kmss.sys.notify.webservice.SendTodo") @ResponseWrapper(localName = "sendTodoResponse", targetNamespace = "http://webservice.notify.sys.kmss.landray.com/", className = "com.landray.kmss.sys.notify.webservice.SendTodoResponse") public NotifyTodoAppResult sendTodo( @WebParam(header = true, name = "appkey") String appkey, @WebParam(name = "arg0", targetNamespace = "") NotifyTodoSendContext arg0) throws Exception_Exception ; }
4、使用http方式請求互動
public static void testGetTodoCount() { ObjectMapper objectMapper = new ObjectMapper(); String urlStr = "http://test.com.cn/restcloud/sys/webservice/sysNotifyTodoWebService?wsdl"; String nameSpaceUrl = "http://webservice.notify.sys.kmss.landray.com/"; NotifyTodoGetCountContext context = new NotifyTodoGetCountContext(); String user = "admin"; context.setTarget("{\"LoginName\":\""+user+"\"}"); context.setTypes("[{\"type\":0}]"); SoapClient client = SoapUtil.createClient(urlStr) .header("appKey", "657963db227be6104fa35a8c") .setMethod("getTodoCount", nameSpaceUrl) .setParams(objectMapper.convertValue(context, Map.class)); String result = client.send(true); result = ReUtil.getGroup0("(<return>).*(</return>)", result); result = HtmlUtil.unescape(result); Map<String, Object> map = XmlUtil.xmlToMap(result); System.out.println(map); }