spring mvc下http发送和接收xml请求

spring mvc下http发送和接收xml请求

本文主要介绍,如何在spring mvc框架下,配合swagger插件,通过http,发送和接受xml请求。

1、通过control编写,接收xml请求的接口,

其中@RequestBody标签,主要存放xml body中的内容看直接传xml内容

其中@RequestHeader  存放xml中的头部内容,主要用于接口安全性校验

2、swagger接口界面,便于用户使用,代码如下:

3、通过http,发送xml请求。

    public String doPostByContent(String url, String param) throws Exception {
        

        StringBuffer parameterBuffer = new StringBuffer();
        parameterBuffer.append(param);
        URL localURL = new URL(url);
        URLConnection connection = openConnection(localURL);
        HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Content-type", "application/json;charset=UTF-8");
        httpURLConnection.setRequestProperty("Accept-Charset", charset);
        httpURLConnection.setRequestProperty("Content-Length", String.valueOf(parameterBuffer.length()));
        OutputStream outputStream = null;
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        StringBuffer resultBuffer = new StringBuffer();
        String tempLine = null;
        
        try {
        	//String s = parameterBuffer.toString();
            outputStream = httpURLConnection.getOutputStream();
            byte[]data = parameterBuffer.toString().getBytes();
            outputStream.write(data);
            if (httpURLConnection.getResponseCode() >= 300) {
                throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
            }
            
            inputStream = httpURLConnection.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream,"utf-8");
            reader = new BufferedReader(inputStreamReader);
            
            while ((tempLine = reader.readLine()) != null) {
                resultBuffer.append(tempLine);
            }
            
        } finally {
            
            if (outputStream != null) {
                outputStream.close();
            }
            
            if (reader != null) {
                reader.close();
            }
            
            if (inputStreamReader != null) {
                inputStreamReader.close();
            }
            
            if (inputStream != null) {
                inputStream.close();
            }
            
        }

        return resultBuffer.toString();
    }
public List listbyTongBuzslt(String foodbarcode,String zchorshxydm,String dateofmanufacture) throws Exception{
		
		String url = "http://syncsoft:8083/spaqjgxt/restful/restfulwebservicecontroller/getXml";
		HttpRequestor requestor = new HttpRequestor();
		String jsonStr = "";
		List list=new ArrayList<>();
		try {

			String xml = "
SPZSXX" + "75E7F5100A91406D9B5FBB5A64AE08E7" + "
8989886234322" + "350203801029057" + " 20170801
"; jsonStr = requestor.doPostByContent(url, xml); list =XmlToStringUtil.listresult(jsonStr); } catch (Exception e) { e.printStackTrace(); } return list; }

xml解析

public static List listresult(String xml) throws Exception {
		List listresult = new ArrayList<>();
		Document document;
		// xml = "  
01 查询成功
20170801 320124000012589 南京脆而爽蔬菜食品有限公司 厦门市思明区味先锋食品商行 厦门市思明区鑫圣豪便利店 20170815 350203801029057 郑州优尚品商贸有限公司 350203310010760 厦门市思明区味先锋食品商行 天素食品有限公司 176074624 郑州优尚品商贸有限公司 350825100004228 84533141 天素食品有限公司
"; document = DocumentHelper.parseText(xml); // 获取根元素 Element root = document.getRootElement(); System.out.println("Root: " + root.getName()); Element bodyEle = root.element("body"); Element logicEle = bodyEle.element("logic"); List datasetList = logicEle.elements(); int count = datasetList.size(); for (int i = 0; i < count; i++) { String qylx = datasetList.get(i).attributeValue("name"); List rowList = datasetList.get(i).elements(); int countj = rowList.size(); for (int j = 0; j < countj; j++) { // scxx String dateofmanufacture = rowList.get(j).elementText( "dateofmanufacture"); String scsregno = rowList.get(j).elementText("scsregno"); String scsshxydm = rowList.get(j).elementText("scsshxydm"); String scsname = rowList.get(j).elementText("scsname"); // lsxx String lssshxydm = rowList.get(j).elementText("lssshxydm"); String gysname = rowList.get(j).elementText("gysname"); String lssname = rowList.get(j).elementText("lssname"); String stockdate = rowList.get(j).elementText("stockdate"); String lssregno = rowList.get(j).elementText("lssregno"); // pfxx String pfgysname = rowList.get(j).elementText("pfgysname"); String pfsregno = rowList.get(j).elementText("pfsregno"); String pfsshxydm = rowList.get(j).elementText("pfsshxydm"); String pfsname = rowList.get(j).elementText("pfsname"); GoodsCirculationZsltEntity gc =new GoodsCirculationZsltEntity(); gc.setQylx(qylx); //scxx gc.setDateofmanufacture(dateofmanufacture); gc.setScsregno(scsregno); gc.setScsshxydm(scsshxydm); gc.setScsname(scsname); //lsxx gc.setLssname(lssname); gc.setLssregno(lssregno); gc.setLssshxydm(lssshxydm); gc.setGysname(gysname); gc.setStockdate(stockdate); //pfxx gc.setPfgysname(pfgysname); gc.setPfsname(pfsname); gc.setPfsregno(pfsregno); gc.setPfsshxydm(pfsshxydm); listresult.add(gc); } } return listresult; }

4 头部存放:

 

你可能感兴趣的:(mysql,java)