Spring框架中接收Http客户端的Post和Get请求

本文主要是介绍了在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;
    }

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值