Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the considerations for receiving parameters using map based on controller

Shulou Source: shulou.com Published: 2022-06-02 21:19:44 09月13日 Update

This article mainly introduces "what are the matters needing attention based on controller using map to receive parameters". In daily operation, I believe many people have doubts about what matters needing attention in using map to receive parameters based on controller. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "what matters needing attention based on controller using map to receive parameters". Next, please follow the editor to study!

Considerations for controller to receive parameters using map

About the problem that the front end uses map to receive parameters

@ PostMapping ("test01") @ ResponseBody / / if @ RequestBody is not added here, then springmvc creates BindAwareModelMappublic Object test01 (Map dataMap) {/ / object by default, and none of the parameters will be encapsulated in System.out.println (dataMap); dataMap = null; return new BindingAwareModelMap (); / / if the BindingAwareModelMap object is returned, an exception will be thrown}

Correct packaging posture

@ Controller@RequestMapping ("map") public class MapController {@ PostMapping ("test01") @ ResponseBody / / if @ RequestBody is added, LinkedHashMap public Object test01 (@ RequestBody Map dataMap) {/ / is created and the parameters are encapsulated (url path parameters and json parameters are encapsulated) System.out.println (dataMap) DataMap.put ("msg", "ojbk"); return dataMap;}}

Conclusion: if you use map to receive front-end parameters, you must add @ Requestbody

# mybatis uses map to encapsulate parameters, @ Select ("select * from t_product where pid = # {pid} or pname = # {pname}") List getByMap (Map map); there is a written method List selectByMap (@ Param ("cm") Map columnMap) in # mybatisplus

Correct encapsulation posture 2

@ Datapublic class Page {private Map dataMap = new HashMap (); / / without initialization, @ RequestBody is added. By default, LinkdedHashMap} @ Controller@RequestMapping ("map") public class MapController {@ PostMapping ("test01") @ ResponseBody public Object test01 (@ RequestBody Page page) {/ / must be added @ RequestBody, otherwise return page;} will not be encapsulated.

The frontend needs to use the format of json to pass parameters:

{"dataMap": {"name": "zzhua"}} controller receives parameters using map and tests with POSTman

Controller layer

@ PostMapping ("/ xksq/getGjclByCondition") public ResultInfo getGjclByCondition (@ RequestBody Map params,HttpServletRequest request) {Map map = new HashMap (); try {Integer iPageIndex = (Integer) params.get ("iPageIndex"); Integer iPageSize = (Integer) params.get ("iPageSize"); PageHelper.startPage Rfgcgl user = rfgcglMapper.selectOne (new QueryWrapper (). Eq ("YHMC", username); if (null==user) {return ResultInfo.fail (903, "user does not exist");} params.put ("qynbbh", user.getQyNbBh ()); List gjclByCondition = clxxQysqMapper.getGjclByCondition (params); PageInfo pageInfo = new PageInfo (gjclByCondition); map.put ("total", pageInfo.getTotal ()) Map.put ("datas", pageInfo); return ResultInfo.ok (map);} catch (Exception e) {e.printStackTrace (); return ResultInfo.fail (901, list condition query failed);}}

Testing with postman

At this point, the study of "what are the considerations for receiving parameters based on controller using map" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Parameters encapsulation matters notes learning front-end posture object method more problem help test utility next no article condition format theory Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Docker Redmi Huawei Microsoft