Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report