本文主要是介绍了在Spring框架中接收Http客户端所传输的参数以及请求
由于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; } }