首页 > 编程语言 >java webservice 带请求头方式处理

java webservice 带请求头方式处理

时间:2024-09-06 13:26:16浏览次数:9  
标签:landray java webservice sys kmss notify com 请求

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

相关文章

  • JavaScript 导出csv
    1.修改指定列为中文可以通过遍历数据对象,将需要转换为中文的字段值替换为中文显示。2.删除不需要的列可以在导出数据之前,删除不需要的列(例如id列)。示例代码exportAsCSV(){//原始数据constdata=[{id:1,name:'JohnDoe',age:28,job:'Engineer'},......
  • IntelliJ IDEA 2024.1.6 (Ultimate Edition)中配置Java Web应用程序
    1. 前期准备工作已经下载安装了JDK(https://www.oracle.com/cn/java/technologies/javase/javase8u211-later-archive-downloads.html)和Tomcat(https://tomcat.apache.org/),并配置了环境变量(安装版的JDK和Tomcat不需要配置环境变量),注意JDK和Tomcat的版本号要相互适应。去官网下......
  • Java什么情况下会Broken pipe java.io.ioexception broken pipe
    最近项目上出现了java.io.IOException:Brokenpipe问题,后边,又出现了org.apache.catalina.connector.ClientAbortException:java.io.IOException:Connectionresetbypeer问题,其实,找到原因后,都是一个问题引起的。错误信息前端浏览器/小程序接口返回报错信息:net::ERR_INCOMPLETE_......
  • #如何解决Tomcat中的应用java.io.IOException您的主机中的一个软件中止了一个已建立的
    错误描述其实就是因为各种原因(比如你调试打的断点,网速等原因导致连接时间超过了默认设置的超时时间tomcat就将该连接断开)org.apache.catalina.connector.ClientAbortException:java.io.IOException:您的主机中的软件中止了一个已建立的连接。1在tomcat出现这个错误是由于客户端......
  • Java中实现数据加密与解密
    加密与解密概述加密是将明文数据转换为密文数据的过程,而解密是将密文数据还原为明文数据的过程。Java提供了丰富的加密解密API,可以实现对称加密、非对称加密和哈希加密等多种加密方式。对称加密对称加密使用相同的密钥进行加密和解密。常见的对称加密算法包括AES、DES等。下面我们......
  • Java I/O (Input/Output)——文件字节流
    JavaI/O简介    JavaI/O(输入/输出)是Java程序中用于处理数据输入和输出的重要部分。输入流(InputStreams):用于从数据源读取数据。常见的输入流包括FileInputStream(从文件读取)、BufferedInputStream(提高读取效率)等。输出流(OutputStreams):用于将数据写入到目的地。例如Fil......
  • Java数据结构---Queue
    队列Queue队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表。入队列(Enqueue):进行插入操作的一端称为队尾出队列(Dequeue):进行删除操作的一端称为队头队列具有先进先出的特性大家可以简单理解为日常生活中“排队”这一现象。队列的模拟实现简单想一想,因为Lin......
  • Java中如何实现minio文件上传
    一、导入minio依赖这里还要导入lombok是因为在MinIOConfig类中使用了@Data注解,正常来说导入minio依赖就够了<dependency>   <groupId>io.minio</groupId>   <artifactId>minio</artifactId>   <version>7.1.0</version> </dependency><dependency......
  • 从内存层面分析Java 参数传递机制
    在Java中,理解参数传递机制对于编写高效和可维护的代码至关重要。本文将探讨基本数据类型和引用数据类型的参数传递方式,并介绍System.identityHashCode方法及其作用。我们将结合栈帧的概念,通过示例代码来详细解释这些机制。System.identityHashCode的作用System.ident......
  • SSH框架整合实现Java三层架构实例(一)
    HTML前台发送请求代码:1<tr>2<td>选择收派时间</td>3<td>4<inputtype="text"name="takeTimeId"class="easyui-combobox"required="true"5data-options="url:'../........