java webservice 带请求头方式处理
1、gradle引入依赖的增强第三方包
1 | implementation 'org.apache.cxf:cxf-spring-boot-starter-jaxws:3.2.6' |
2、增强类方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | 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客户端定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | 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方式请求交互
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具