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