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); }
标签:landray,java,webservice,sys,kmss,notify,com,请求 From: https://www.cnblogs.com/g177w/p/18400045