如何在cxf中获取soap报文,工作中

解答在CXF google中有一个帖子“请问如何用cxf在请求中加header信息“

解析soap报文 sop报文和soap报文解析soap报文 sop报文和soap报文


解析soap报文 sop报文和soap报文


解析soap报文 sop报文和soap报文


由于需要记录访问servs的client。需要从SOAP消息中获取Header的数据。具体的方法如下:

server:

Ja代码

public class LoggingInterceptor extends AbstractPhaseInterceptor {

Log logger = LogFactory.getLog(LoggingInterceptor.class);

private SAAJInInterceptor saajIn = new SAAJInInterceptor();

public LoggingInterceptor() {

super(Phase.PRE_PROTOCOL);

getAfter().add(SAAJInInterceptor.class.getName());

}public void handleMessage(SoapMessage message) throws Fault {

try {

logger.("记录访问web servs日志");

SOAPMessage doc = message.getContent(SOAPMessage.class);

if (doc == null) {

saajIn.handleMessage(message);

doc = message.getContent(SOAPMessage.class);

}SOAPHeader header = doc.getSOAPHeader();

if (header == null) {

return;

}NodeList nodes = header.getElementsByTagName("proc:user");

for(int i=0; i

System.out.println(nodes.(i).getLocalName()+"----"+nodes.(i).getTextContent());

}} catch (SOAPException e) {

e.printStackTrace();

}}

}

JAVA 如何解析soap

SAXReader reader = new SAXReader(); Document document = reader.read(file.getInputStream()); Element root document.getRootElement();

Element header = root.element("RequestData");

在根据名称逐步获取

我觉得你的问题应该不是说如何解析这个xml吧,应该说如何吧这个xml转换为一个实际的ja类调用把,那这个太多了,建议cxf吧。可以做soap的客户端也可以做服务端,不需要你写多少东西

soap报文异常

网络问题。soap报文异常是网络问题,更换网络即可。SOAP一般指简单对象访问协议。简单对象访问协议是交换数据的一种协议规范,是一种轻量的、简单的、基于XML(标准通用标记语言下的一个子集)的协议,它被设计成在WEB上交换结构化的和固化的信息。

soap中base64Binary解码

public class Base64File {

public static int MAX_BUFFER_SIZE = 256000;

/

对二进制文件进行编码,返回Base64编码的字符串

@param filePath 文件路径

@return Base64编码的字符串

/

public static String fileToBase64String(String filePath) throws IOException,FileNotFoundException{

InputStream in = null;

int fileLength = -1;

StringBuffer str = new StringBuffer();

try {

File f = new File(filePath);

if (!f.isFile())

return null;

in = new FileInputStream(f.getPath());

byte buffer[] = new byte[MAX_BUFFER_SIZE];

int read = 0;

fileLength = 0;

while ((read = in.read(buffer)) != -1) {

fileLength += read;

if (read < MAX_BUFFER_SIZE) // if buffer size < max. size :-)

{byte[] buf = new byte[read];

System.arraycopy(buffer, 0, buf, 0, read);

buffer = buf;

}//可依据不同文件大小选择sun或者apache的Base64解决方案,小文件sun快,大文件apache快。

//str.append(new String(Base64.encodeBase64(buffer)));

str.append(new BASE64Encoder().encode(buffer)+"");

}} finally {

try {

if (in != null)

in.close();

} catch (Exception e) {

//e.printStackTrace();

}}

return str.toString();

}/

将Base64编码的字符串还原为二进制文件

@param filePath 二进制文件路径

@param data Base64编码的文本

@return 是否成功

/

public static boolean base64StringToFile(String filePath, String data) throws IOException,FileNotFoundException,Exception{

OutputStream os = null;

try {

os = new FileOutputStream(new File(filePath));

//byte[] buf = Base64.decodeBase64(data.getBytes());

byte[] buf = new BASE64Decoder().decodeBuffer(data);

if (buf != null) {

os.write(buf);

} else{

throw new Exception("Base64 image data invalid.");

}return true;

} finally {

try {

if (os != null)

os.close();

} catch (Exception e) {

//e.printStackTrace();

}}

}public static String base64Encode(byte[] data) throws IOException{

if(data==null||data.length==0) return null;

InputStream in = null;

int fileLength = -1;

StringBuffer str = new StringBuffer();

try {

in = new ByteArrayInputStream(data);

byte buffer[] = new byte[MAX_BUFFER_SIZE];

int read = 0;

fileLength = 0;

while ((read = in.read(buffer)) != -1) {

fileLength += read;

if (read < MAX_BUFFER_SIZE) // if buffer size < max. size :-)

{byte[] buf = new byte[read];

System.arraycopy(buffer, 0, buf, 0, read);

buffer = buf;

}//可依据不同文件大小选择sun或者apache的Base64解决方案,小文件sun快,大文件apache快。

//str.append(new String(Base64.encodeBase64(buffer)));

str.append(new BASE64Encoder().encode(buffer)+"");

}} finally {

try {

if (in != null)

in.close();

} catch (Exception e) {

//e.printStackTrace();

}}

return str.toString();

}