好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

基于controller使用map接收参数的注意事项

controller使用map接收参数注意事项

关于前端使用map去接收参数的问题

?

1

2

3

4

5

6

7

@PostMapping ( "test01" )

@ResponseBody                         // 如果这里不加@RequestBody,那么springmvc默认创建的是BindAwareModelMap

public Object test01( Map dataMap) { // 对象,并且都参数都不会封装进去

     System.out.println(dataMap);

     dataMap = null ;

     return new BindingAwareModelMap(); // 如果返回BindingAwareModelMap对象,就会抛出异常

}

正确封装姿势1

?

1

2

3

4

5

6

7

8

9

10

11

@Controller

@RequestMapping ( "map" )

public class MapController {

     @PostMapping ( "test01" )

     @ResponseBody                                     // 如果加了@RequestBody,那么创建的是LinkedHashMap

     public Object test01( @RequestBody Map dataMap) { // 并且参数都封装了进去(url路径参数和json参数都会封装进去)

         System.out.println(dataMap);

         dataMap.put( "msg" , "ojbk" );

         return dataMap;

     }

}

结论:如果使用map接收前端参数,那么一定要加@Requestbody才行

?

1

2

3

4

5

#mybatis使用map封装参数,

@Select ( "select * from t_product where pid = #{pid} or pname = #{pname}" )

List<Product> getByMap(Map map);   

#mybatisplus中有写好的方法

List<T> selectByMap( @Param ( "cm" ) Map<String, Object> columnMap);

正确封装姿势2

?

1

2

3

4

5

6

7

8

9

10

11

12

13

@Data

public class Page {

     private Map dataMap = new HashMap(); // 这里可以不用初始化,加了@RequestBody,默认创建LinkdedHashMap

}

@Controller

@RequestMapping ( "map" )

public class MapController {

     @PostMapping ( "test01" )

     @ResponseBody

     public Object test01( @RequestBody Page page) { // 一定要加@RequestBody,否则封装不进去

         return page;

     }

}

前端需要使用json传参格式:

?

1

2

3

4

5

{

     "dataMap" :{

         "name" : "zzhua"

     }

}

controller使用map接收参数并用POSTman测试

controller层

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

@PostMapping ( "/xksq/getGjclByCondition" )

public ResultInfo getGjclByCondition( @RequestBody Map<String,Object> params,HttpServletRequest request){

     Map<String,Object> map = new HashMap<>();

     try {

         Integer iPageIndex = (Integer) params.get( "iPageIndex" );

         Integer iPageSize = (Integer) params.get( "iPageSize" );

         PageHelper.startPage(iPageIndex!= null ?iPageIndex: 1 ,iPageSize!= null ?iPageSize: 10 );

         String username = JwtUtil.getUsername(request.getHeader( "token" ));

         Rfgcgl user = rfgcglMapper.selectOne( new QueryWrapper<Rfgcgl>().eq( "YHMC" , username));

         if ( null ==user){

             return ResultInfo.fail( 903 , "用户不存在" );

         }

         params.put( "qynbbh" ,user.getQyNbBh());

         List<Map<String, Object>> gjclByCondition = clxxQysqMapper.getGjclByCondition(params);

         PageInfo<Map<String, Object>> 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 , "列表条件查询失败" );

     }

}

使用postman测试

controller使用map接收参数时必须使用 @RequestBody接收参数,否则后台会出现接收不到的情况

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/qq_16992475/article/details/107179019

查看更多关于基于controller使用map接收参数的注意事项的详细内容...

  阅读:23次