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

编程知识 更新时间:2023-04-04 04:45:10

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

}


更多推荐

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

本文发布于:2023-04-04 04:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/af310b249e348209f8bc05b3d881d63c.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   框架   Spring   Post   Http

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 41205文章数
  • 14阅读数
  • 0评论数