使用springmvc编写restful接口
2016-02-17 16:22
357 查看
使用HttpURLConnection创建conn连接
传递数据方式:post请求方式
下面请求
public static String postHttpConn (){
HttpURLConnection conn = null;
try {
// 创建一个URL对象
URL url = new URL("http://127.0.0.1:8080/soc/infoMonitor/testpost");
// 调用URL的openConnection()方法,获取HttpURLConnection对象
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");// 设置请求方法为post
conn.setReadTimeout(5000);// 设置读取超时为5秒
conn.setConnectTimeout(10000);// 设置连接网络超时为10秒
conn.setDoOutput(true);// 设置此方法,允许向服务器输出内容
// post请求的参数
String data = "message=" + packageXml();//该方法时我自己创建的xml报文数据类型的字符串
// 获得一个输出流,向服务器写数据,默认情况下,系统不允许向服务器输出内容
OutputStream out = conn.getOutputStream();// 获得一个输出流,向服务器写数据
out.write(data.getBytes());
out.flush();
out.close();
int responseCode = conn.getResponseCode();// 调用此方法就不必再使用conn.connect()方法
if (responseCode == 200) {
InputStream is = conn.getInputStream();
String returnmsg= getStringFromInputStream(is);
System.out.println(returnmsg);//返回的数据
} else {
System.out.println("shibai");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();// 关闭连接
}
}
return "yes";
}
下面是springmvc接收请求的代码
// 解析结单返回数据
@RequestMapping(value = "/testpost" , method=RequestMethod.POST , produces = "application/xml; charset=utf-8")
public @ResponseBody String postReturn(String message){
// 解析xml数据,使用jdk自带工具
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
Document doc = null;
try {
builder = factory.newDocumentBuilder();
doc = builder.parse(new ByteArrayInputStream(message.getBytes()));
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 解析xml数据,使用jdk自带工具
NodeList nl = doc.getElementsByTagName("RECEIPTINFO");
for (int i=0;i<nl.getLength() ; i++){
System.out.println("编号:" + doc.getElementsByTagName("NUMBER").item(0).getFirstChild().getNodeValue());
System.out.println("联系人:" + doc.getElementsByTagName("CONTACT").item(0).getFirstChild().getNodeValue() );
System.out.println("联系方式:" + doc.getElementsByTagName("CONTACTINFO").item(0).getFirstChild().getNodeValue() );
System.out.println("详情信息:" + doc.getElementsByTagName("DEALDETAIL").item(0).getFirstChild().getNodeValue() );
System.out.println("单位):" + doc.getElementsByTagName("UNIT").item(0).getFirstChild().getNodeValue() );
System.out.println("部门:" + doc.getElementsByTagName("DEPARTMENT").item(0).getFirstChild().getNodeValue() );
System.out.println("状态):" + doc.getElementsByTagName("STATE").item(0).getFirstChild().getNodeValue() );
}
StringBuilder xmlReturn = new StringBuilder();
xmlReturn.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>").append("\n");
xmlReturn.append("<RETURNINFO>").append("\n");
xmlReturn.append("
<STATE>").append("发送成功").append("</STATE>").append("\n");
xmlReturn.append("</RETURNINFO>");
return xmlReturn ;
}
@RequestMapping标签中的定义了字符的编码格式,避免接收方接收数据乱码
/**
* 根据流返回一个字符串信息
* @param is
* @return
* @throws IOException
*/
private static String getStringFromInputStream(InputStream is) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
is.close();
String message = os.toString();
os.close();
return message;
}
传递数据方式:post请求方式
下面请求
public static String postHttpConn (){
HttpURLConnection conn = null;
try {
// 创建一个URL对象
URL url = new URL("http://127.0.0.1:8080/soc/infoMonitor/testpost");
// 调用URL的openConnection()方法,获取HttpURLConnection对象
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");// 设置请求方法为post
conn.setReadTimeout(5000);// 设置读取超时为5秒
conn.setConnectTimeout(10000);// 设置连接网络超时为10秒
conn.setDoOutput(true);// 设置此方法,允许向服务器输出内容
// post请求的参数
String data = "message=" + packageXml();//该方法时我自己创建的xml报文数据类型的字符串
// 获得一个输出流,向服务器写数据,默认情况下,系统不允许向服务器输出内容
OutputStream out = conn.getOutputStream();// 获得一个输出流,向服务器写数据
out.write(data.getBytes());
out.flush();
out.close();
int responseCode = conn.getResponseCode();// 调用此方法就不必再使用conn.connect()方法
if (responseCode == 200) {
InputStream is = conn.getInputStream();
String returnmsg= getStringFromInputStream(is);
System.out.println(returnmsg);//返回的数据
} else {
System.out.println("shibai");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();// 关闭连接
}
}
return "yes";
}
下面是springmvc接收请求的代码
// 解析结单返回数据
@RequestMapping(value = "/testpost" , method=RequestMethod.POST , produces = "application/xml; charset=utf-8")
public @ResponseBody String postReturn(String message){
// 解析xml数据,使用jdk自带工具
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
Document doc = null;
try {
builder = factory.newDocumentBuilder();
doc = builder.parse(new ByteArrayInputStream(message.getBytes()));
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 解析xml数据,使用jdk自带工具
NodeList nl = doc.getElementsByTagName("RECEIPTINFO");
for (int i=0;i<nl.getLength() ; i++){
System.out.println("编号:" + doc.getElementsByTagName("NUMBER").item(0).getFirstChild().getNodeValue());
System.out.println("联系人:" + doc.getElementsByTagName("CONTACT").item(0).getFirstChild().getNodeValue() );
System.out.println("联系方式:" + doc.getElementsByTagName("CONTACTINFO").item(0).getFirstChild().getNodeValue() );
System.out.println("详情信息:" + doc.getElementsByTagName("DEALDETAIL").item(0).getFirstChild().getNodeValue() );
System.out.println("单位):" + doc.getElementsByTagName("UNIT").item(0).getFirstChild().getNodeValue() );
System.out.println("部门:" + doc.getElementsByTagName("DEPARTMENT").item(0).getFirstChild().getNodeValue() );
System.out.println("状态):" + doc.getElementsByTagName("STATE").item(0).getFirstChild().getNodeValue() );
}
StringBuilder xmlReturn = new StringBuilder();
xmlReturn.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>").append("\n");
xmlReturn.append("<RETURNINFO>").append("\n");
xmlReturn.append("
<STATE>").append("发送成功").append("</STATE>").append("\n");
xmlReturn.append("</RETURNINFO>");
return xmlReturn ;
}
@RequestMapping标签中的定义了字符的编码格式,避免接收方接收数据乱码
/**
* 根据流返回一个字符串信息
* @param is
* @return
* @throws IOException
*/
private static String getStringFromInputStream(InputStream is) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
is.close();
String message = os.toString();
os.close();
return message;
}
相关文章推荐
- Java学习笔记 2016.2.17 方法重载、继承剖析
- ie浏览器 spring mvc返回json数据弹出下载页面 解决方案
- MyEclipse部署文件被lock的问题
- 不错的Spring学习笔记(转)
- java术语(PO/POJO/VO/BO/DAO/DTO)
- SpringMvc与Struts2比较
- Win7下的java环境变量配置
- eclipse无法访问genymotion模拟器下/data/data文件解决方案
- Java ClassLoader机制
- java实现mysql数据库备份与还原
- Java读取二进制文件
- 设置Eclipse中的tab键为4个空格的完整方法
- java单例模式的5种实现
- 使用JAVA发现的一些小问题
- java打印帕斯卡三角形
- Struts2 新特性
- Java笔记---生成器Generator
- java后台图片大小压缩
- Java学习笔记2016.2.17 方法参数传递
- JList监听,选择会得到两次事件