java后端接收http请求参数

如果请求参数json

1.以@RequestParam接收

 @PostMapping("ccc1")
    public String ccc1(@RequestParam("name") String name) {
        return name;
    }

2.以实体类方式接收

@PostMapping("ccc2")
    public String getList(@RequestBody TestUser user) {
        return "success";
    }

3.以map接收

 @PostMapping("ccc")
    public boolean ccc3(@RequestBody Map<String,Object> map) {
        if (map.containsKey("name")){
            String name = map.get("name").toString();
            boolean b = testUserService.selectAllByName(name);
            return b;
        }else {
            return false;
        }
    }

 4.List接收

 当前端传来这样一个json数组:[{id,username,password},{id,username,password},    
    {id,username,password},...]时,用List<E>接收

   @PostMapping("getList")
    public String getList(@RequestBody List<TestUser> list) {
        for (TestUser user : list) {
            System.out.println(user.toString());
        }
        return "success";
    }

 

5.常规单个参数list?username=bb&password=123

 @RequestMapping("list")
    public String list(String username,String password) {
            System.out.println(username+" "+password);
        return "success";
    }

6.以xml以为参数

    @RequestMapping("/wcJsPayCallback")
    public void wcJsPayCallback(HttpServletRequest req, HttpServletResponse res ) throws Exception {
        HashMap<String, String> retMap = MessageUtil.parseXmlToHashMap(req);
        log.debug("--------------------------收到支付回调--------------------------------------------");
        log.debug(XMLUtil.mapToXml(retMap));
        log.debug("----------------------------------------------------------------------------------");
  }

MessageUtil类parseXmlToHashMap办法

@SuppressWarnings("unchecked")
public static HashMap<String, String> parseXmlToHashMap(HttpServletRequest request)
throws Exception {
// 将解析结果存储在HashMap中
HashMap<String, String> map = new HashMap<String, String>();


// 从request中取得输入流
InputStream inputStream = request.getInputStream();
// 读取输入流
SAXReader reader = new SAXReader();
Document document = reader.read(inputStream);
// 得到xml根元素
Element root = document.getRootElement();
// 得到根元素的所有子节点
List<Element> elementList = root.elements();


// 遍历所有子节点
for (Element e : elementList)
map.put(e.getName(), e.getText());


// 释放资源
inputStream.close();
inputStream = null;


return map;
}



 7.直接post json参数{"request":"1","request2":"2","request2":"2"}

    @RequestMapping("/callback")
    public String  callback(@RequestBody String request, HttpServletRequest req, HttpServletResponse res) throws Exception {
        
        log.debug("--收到银行支付回调start--");
        try{
              log.debug("requestContent:"+ request);
}

 8.直接post json参数{"request":"1","request2":"2","request2":"2"}

  @RequestMapping("/callbackV3")
    @ResponseBody
    public WcJsapiCallbackV3ParamsBean callbackV3(HttpServletRequest req, HttpServletResponse res ) throws Exception {
   
        log.debug("--------------------------微信支付分callbackV3通知--------------------------------------------");
            String json = org.apache.commons.io.IOUtils.toString(req.getInputStream());  

}

 

部分内容引用于:https://www.csdn.net/tags/NtDaEgysNDQ3NTktYmxvZwO0O0OO0O0O.html

posted @ 2022-07-27 17:25  幸福眼泪  阅读(3087)  评论(0编辑  收藏  举报