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

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

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

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

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

前半部分校验接口,后半部分解析处理xml数据,xml可以用dom4j 解析


代码如下:
2、swagger接口界面,便于用户使用,代码如下:
3、通过http,发送xml请求。

 public String doPostByContent(String url, String param,Map<String,Object> headerMap) throws Exception {

        

        /* Translate parameter map to parameter date string */

        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()));

        

        

        if (headerMap !=null) {

            Iterator<String> iterator = headerMap.keySet().iterator();

            String key = null;

            String value = null;

            while (iterator.hasNext()) {

                key = (String)iterator.next();

                if (headerMap.get(key) !=null) {

                    value = (String)headerMap.get(key);

                } else {

                    value = "";

                }

                

                httpURLConnection.setRequestProperty(key,value);

            }

        }

        

        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();

    }

4 头部存放:

猜你喜欢

转载自blog.csdn.net/lq18050010830/article/details/78851462