Spring框架中接收Http客户端的Post和Get请求
2017-08-31 15:59
393 查看
本文主要是介绍了在Spring框架中接收Http客户端所传输的参数以及请求
由于Spring框架可以与数据库mybatis以及Hibernate较好的集成,使用接收的数据更易储存到数据库中
同时能够通过HTTP请求从数据库中查询获取需要的信息
因为Http的请求和Web请求一样都是通过URL来访问,所以也将其放在Controller层
在方法结束后加入Return可以直接向Client端发送返回信息
以下是具体的实现代码:
与Post方法不同的是Get方法还是使用HttpServletRequest的方式获取到URL的参数
由于Get的请求方式是将参数放在URL/后面
由于Spring框架可以与数据库mybatis以及Hibernate较好的集成,使用接收的数据更易储存到数据库中
同时能够通过HTTP请求从数据库中查询获取需要的信息
因为Http的请求和Web请求一样都是通过URL来访问,所以也将其放在Controller层
在方法结束后加入Return可以直接向Client端发送返回信息
以下是具体的实现代码:
package com.firstdata.opensystem.action; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.firstdata.opensystem.service.FileConfigService; import com.firstdata.opensystem.service.FileReportService; import com.firstdata.opensystem.vo.FileConfig; import net.sf.json.JSONObject; @Controller @RequestMapping("/MsgReceive") public class MsgReceiveAction { private static Logger logger = Logger.getLogger(MsgReceiveAction.class); @Resource private FileReportService fileReportService; @Resource private FileConfigService fileConfigService; @RequestMapping(value = "", method = RequestMethod.POST) @ResponseBody public String msgReceive(@RequestBody String Msg) { System.out.println(Msg); JSONObject obj = JSONObject.fromObject(Msg); fileReportService.addNewFileReport(obj); logger.info("Receive File Report request for " + obj.getString("fileCode")); return "000"; }
与Post方法不同的是Get方法还是使用HttpServletRequest的方式获取到URL的参数
由于Get的请求方式是将参数放在URL/后面
@RequestMapping(value = "", method = RequestMethod.GET) @ResponseBody public String recordInquiry(HttpServletRequest request) { String fileCode = request.getParameter("fileCode"); String bizDate = request.getParameter("bizDate"); boolean inquiryFlag = fileReportService.inquiryFileReport(fileCode, bizDate); FileConfig fileConfig = fileConfigService.getFileConfigByFileCode(fileCode); String respMsg = String.valueOf(inquiryFlag)+" = "+fileConfig; logger.info("Inquire whether " + fileCode + " has existed. Result: " + respMsg); return respMsg; } }
相关文章推荐
- JAVA Socket 实现HTTP与HTTPS客户端发送POST与GET方式请求
- JAVA Socket 实现HTTP与HTTPS客户端发送POST与GET方式请求
- JAVA Socket 实现HTTP与HTTPS客户端发送POST与GET方式请求
- JAVA Socket 实现HTTP与HTTPS客户端发送POST与GET方式请求
- JAVA Socket 实现HTTP与HTTPS客户端发送POST与GET方式请求
- C# winform端 通过HttpWebRequest进行post和get请求,数据格式为json,后台java端接收,其中有关传输特殊字符(\t,\r,',\n,n)等处理
- AngularJs中$http发送post或者get请求,SpringMVC后台接收不到参数值的解决办法
- HTTP客户端发送Post和Get请求
- 使用JavaScript的XMLHttpRequest发送POST、GET请求以及接收返回值
- python 抓http GET POST 客户端请求包(关键的是规则)
- XMLHttpRequest发送POST、GET请求以及接收返回值
- AngularJs中$http发送post或者get请求,SpringMVC后台接收不到参数值的解决办法
- 客户端与服务端Post报文构造请求及Http Post与Get请求方法
- Node.js中http 接收get请求、接收post请求、发送get请求、发送post请求
- httpUrlConnection post请求 传参数 及客户端接收参数
- 使用JavaScript的XMLHttpRequest发送POST、GET请求以及接收返回值
- JAVA Socket 实现HTTP与HTTPS客户端发送POST与GET方式请求
- HttpURLConnection 向服务器发送post和get请求 并接收响应
- 【网络】JAVA Socket 实现HTTP与HTTPS客户端发送POST与GET方式请求
- JAVA Socket 实现HTTP与HTTPS客户端发送POST与GET方式请求