soap设置报文头值 java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了soap设置报文头值 java相关的知识,希望对你有一定的参考价值。

经过了wsdl文件解析,现有报文为
<soapenv:envlope>
<soapenv:body>
字段值…………
</soapenv:body>
</soapenv:envlope>
由于现在需求,需要soap报文头设置一些字段,也就是在<soapenv:header></soapenv:header>里设置字段,用java实现,请教会的大神!

首先,通过流的方式读取txt中soap报文字符串。第二步,通过dom4j读取soap报文字符串进行解析(常规的xml解析)追问

有没有这方面相关例子

参考技术A 可以通过jaxb来解析吧。百度有很多jaxb教程,可以参考一下

xml字符串和java实体类相互转换JaxbXmlUtil工具类 附java实体类生成soap接口报文案例

JaxbXmlUtil工具类

package com.aiait.ivs.util;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;





/**
 * Jaxb工具类 xml和java类相互转换
 *
 * @author sunjianbo
 * @date 2017年4月17日
 */
public class JaxbXmlUtil {

    public static final String DEFAULT_ENCODING = "UTF-8";

    /**
     * pojo转换成xml 默认编码UTF-8
     *
     * @param obj 待转化的对象
     * @return xml格式字符串
     * @throws Exception JAXBException
     */
    public static String convertToXml(Object obj) throws Exception {
        return convertToXml(obj, DEFAULT_ENCODING);
    }

    /**
     * pojo转换成xml
     *
     * @param obj 待转化的对象
     * @param encoding 编码
     * @return xml格式字符串
     * @throws Exception JAXBException
     */
    public static String convertToXml(Object obj, String encoding) throws Exception {
        String result = null;

        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        // 指定是否使用换行和缩排对已编组 XML 数据进行格式化的属性名称。
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);

        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        result = writer.toString();

        return result;
    }

    /**
     * xml转换成JavaBean
     *
     * @param xml xml格式字符串
     * @param t 待转化的对象
     * @return 转化后的对象
     * @throws Exception JAXBException
     */
    @SuppressWarnings("unchecked")
    public static <T> T convertToJavaBean(String xml, Class<T> t) throws Exception {
        T obj = null;
        JAXBContext context = JAXBContext.newInstance(t);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        obj = (T) unmarshaller.unmarshal(new StringReader(xml));
        return obj;
    }
}

生成xml的实体类

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
        "header",
        "body"
    })
@XmlRootElement(name = "soapenv:Envelope")
public class RequestEntity {

    @XmlAttribute(name="xmlns:soapenv")
    protected String soapenv="http://schemas.xmlsoap.org/soap/envelope/";
    @XmlAttribute(name="xmlns:eapp")
    protected String eapp="http://wx.gmw9.com";
    
    @XmlAttribute(name="xmlns:bus")
    protected String bus=null;
    
    
    
    @XmlElement(required = true,name="soapenv:Header")
    protected RequestHeader header;
    
    @XmlElement(required = true,name="soapenv:Body")
    protected RequestBody body;

    
    //get set方法省略

    
}




import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "soapenv:Body")
public class RequestBody {

    
    @XmlElement(required = true,name="eapp:aaaaaa")
    public BodyContent aaaaaaa
    //get set方法省略
    
}




import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "aa1",
    "aa2",
    "aa3"
   
})
@XmlRootElement(name = "eapp:aaaaaa")
public class BodyContent {

          @XmlElement(required = true,name="eapp:compNo")
        protected String aa1;
        @XmlElement(required = true,name="eapp:agentCode")
        protected String aa2;
        @XmlElement(required = true,name="eapp:eAppId")
        protected String aa3;              
        //get set方法省略
    
}

生成的xml如下

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:eapp="http://wx.gmw9.com">
   <soapenv:Header>
      
   </soapenv:Header>
   <soapenv:Body>
      <eapp:aaaaaa>
         <eapp:aa1>041</eapp:aa1>
         <eapp:aa2>5000</eapp:aa2>
         <eapp:aa3>?</eapp:aa3>
         
      </eapp:aaaaaa>
   </soapenv:Body>
</soapenv:Envelope>

 

以上是关于soap设置报文头值 java的主要内容,如果未能解决你的问题,请参考以下文章

java怎么解析文本文件中的soap报文

SOAP报文转成JAVA对象

java 查看SOAP请求报文

发送soap信息失败是啥意思

java使用POST发送soap报文请求webservice返回500错误解析

java如何打印webservice 发送的soap报文协议