栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA使用Controller接受soap请求,转化为对象

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA使用Controller接受soap请求,转化为对象

 soap文档

http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?disco




soap协议:简单对象访问协议是交换数据的一种协议规范,是一种轻量的、简单的、基于XML(标准通用标记语言下的一个子集)的协议,它被设计成在WEB上交换结构化的和固化的信息;

可以把它堪称一个xml ,但是有不规则的字段节点

1.首先把这个xml文件转化为json格式(注意引入的maven,org.json)
package com.shands;

import com.alibaba.fastjson.JSON;
import com.shands.demo.Demo;
import org.json.JSONObject;
import org.json.XML;

import java.io.*;


public class Demo1 {
    public static void main(String[] args) {
        String path = "D:cann.xml.xml";

        String s = readTxt(path);
        JSonObject soapDatainJsonObject = XML.toJSonObject(s);
        String s1 = soapDatainJsonObject.toString();
        System.out.println(s1);

    }
    public static String readTxt(String path) {
        StringBuilder content = new StringBuilder();
        try {
            String code = resolveCode(path);
            File file = new File(path);
            InputStream is = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(is, code);
            BufferedReader br = new BufferedReader(isr);
            String str = "";
            while (null != (str = br.readLine())) {
                content.append(str);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("读取文件:" + path + "失败!");
        }
        return content.toString();

    }

    public static String resolveCode(String path) throws Exception {
        InputStream inputStream = new FileInputStream(path);
        byte[] head = new byte[3];
        inputStream.read(head);
        String code = "gb2312";  //或GBK
        if (head[0] == -1 && head[1] == -2)
            code = "UTF-16";
        else if (head[0] == -2 && head[1] == -1)
            code = "Unicode";
        else if (head[0] == -17 && head[1] == -69 && head[2] == -65)
            code = "UTF-8";

        inputStream.close();

        System.out.println(code);
        return code;
    }
}
2.优化json文件,(如果有不规则的节点名把不规则的节点名去掉如soap: hc: 之类的)

3.复制json 文件转为对象

 

 

4.生成完整的类对象

(如果有前缀,那么在jsonfiled 中加上 如

@JSonField(name = "xmlns:soap")
private String _$XmlnsSoap53;

5.在controller 中用string 或者HttpServletRequest接受到xml

6.使用org.jsonJSONObject 转为对象
org.json.JSonObject soapDatainJsonObject = XML.toJSonObject(xml);
String s1 = soapDatainJsonObject.toString();
demo2 demo= JSON.parseObject(s1, demo2 .class);

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/425637.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号