小结 :各种参数解析,其实就是 springmvc 适配性 的作用 叫做参数解析器,根据类型 匹配对应的数据 

1.X 都是表单格式默认 为application/x-wwww-from-urlencoded 提交
2.X 都是 json 格式application/json 其实就是内部jackson 序列化对象

1.1 常规类型匹配 代码如下 

      /**

* 常规类型匹配
 * @param name 字符串
 * @param leader 0,false 都会解析成false 1 或者true 都会解析 true
 * @param id
 * @param age
 * @param price
 * @param em 枚举类型 如传 SUCCESS 串自己定义的枚举大写
 * @return
 */
@PostMapping("dept/update")
public ResponseResult updateDept(String name, Boolean leader, Integer id
                                , Double age, BigDecimal price, ResponseResultCode em){

    return ResponseResultFactory.success("添加成功");
}

1.2 集合类型
/**
 * 集合类型 必须加 RequestParam 注解 不然报错  No primary or default constructor found for interface
 * @param ids 可以是 传 1,2,3 逗号分隔 也可以 ids=1&ids=2 但是不能混合传 解析器问题
 * @return
 */
@PostMapping("dept/update/ids")
public ResponseResult updateDept(@RequestParam(value = "ids") List<Integer> ids){

    return ResponseResultFactory.success("添加成功");
}
1.3 数组类型
/**
 * 数组和和集合一样
 * @param ids
 * @return
 */
@PostMapping("dept/update/ids1")
public ResponseResult updateDept(@RequestParam(value = "ids") Integer[] ids){

    return ResponseResultFactory.success("添加成功");
}
1.4 map 类型
/**
 * map 必须加上 @RequestParam 注解 不然 没数据 看上去map 万能接受 但是不推荐使用(除非不知道前端字段可以考虑)
 * @param map
 * @return
 */
@PostMapping("dept/update/map")
public ResponseResult updateDeptMap(@RequestParam Map map){

    return ResponseResultFactory.success("添加成功");
}
1.5 对象类型
/**
 * 普通对象类型
 * @param user 如 id=12&name=小明 简单传就行
 * @return
 */
@PostMapping("dept/addUser")
public ResponseResult addDept(User user){

    return ResponseResultFactory.success("添加用户");
}
1.6 复杂对象 对象里面套对象
/**
 * 复杂对象 @RequestParam 不要用这个注解 不然匹配不上名字
 * @param dept users[0].name=aaa&users[0].age=12&users[1].name=13&users[1].age=12&deptName=呵呵呵&ids=1,2,3
 * @return
 */
@PostMapping("dept/Dept")
public ResponseResult addDept(Dept dept){

    return ResponseResultFactory.success("添加用户");
}
1.7 集合对象 第一种不适合 可以考虑 下面 string 转换实现
@PostMapping("dept/users")
public ResponseResult addDept(@RequestParam(value = "users")List<User> users){

    return ResponseResultFactory.success("添加用户");
}
/**
 * 集合对象
 * @param users
 * @return
 */
@PostMapping("dept/users1")
public ResponseResult addDept1(String users){
    // 在转换一次 json 工具
    return ResponseResultFactory.success("添加用户");
}

2.1  对象类型
/**
 * 对象
 *  header格式 content-type:"application/json charset=utf-8", 参数 直接json 格式
 *  {"id": 1,name": "demoData","age": 1,"pthone": "demoData",email": "demoData"}
 *
 *   $.ajax({
 url: "srarch_data/search_data_detail.action",
 type: "post",
 dataType:'json',
 contentType : 'application/json;charset=utf-8',
 data:  JSON.stringify(obj),
 success: function (json) {

 }
 });
 * @param user
 * @return
 */
@PostMapping("addUser")
public ResponseResult addUser(@RequestBody User user){

    return ResponseResultFactory.success("添加用户");
}
2.2 集合 类型
/**
 * 集合
 * @param users  [{"id": 1,"name": "demoData","age": 1,"pthone": "demoData","email": "demoData"},{"id": 1,"name": "demoData","age": 1,"pthone": "demoData","email": "demoData"}]
 * @return
 */
@PostMapping("addUsers")
public ResponseResult addUsers(@RequestBody List<User> users){

    return ResponseResultFactory.success("添加用户");
}
2.3 复杂对象 传递
/**
 *复杂对象 传递
 * {
 "id": 1,
 "deptName": "demoData",
 "users": [{
 "id": 1,
 "name": "demoData",
 "age": 1,
 "pthone": "demoData",
 "email": "demoData"
 }],
 "createDate": "demoData",
 "updateTime": "2019/6/3 12:12",
 "ids": [
 1
 ]
 }
 * @param dept
 * @return
 */
@PostMapping("addDept")
public ResponseResult addDept2(@RequestBody Dept dept){

    return ResponseResultFactory.success("添加用户",dept);
}
2.4 map 类型
json 格式

springboot 自定义HandlerExecutionChain springboot自定义参数解析_json

 

 表单模式

 

springboot 自定义HandlerExecutionChain springboot自定义参数解析_User_02

 

 

/**
 *  可以格式任意数据 @RequestParam 这个注解 可以不要 如果加了 @RequestBody, 但是表单格式必须加  @RequestParam
 * @param dept
 * @return
 */
@PostMapping("addDeptMap")
public ResponseResult addDept3(@RequestParam @RequestBody Map dept){

    return ResponseResultFactory.success("添加用户",dept);
}
2.5 混合模式 基本很少人折磨用 但是可以用
/**
 * ================================== 表单模式和 json 格式混用 header 还是application/json  url 上带参数 =====================================
 */
/**
 * 混合模式 基本很少人折磨用 但是可以用
 * @param user
 * @param id
 *
 * 例如 /mix?id=12  {"id": 1,"name": "demoData","age": 1,"pthone": "demoData","email": "demoData"}
 * @return
 */
@PostMapping("mix")
public ResponseResult mix(@RequestBody User user,Integer id){

    return ResponseResultFactory.success("添加用户");
}
2.6 自定义 序列化 与反序列化
/**
 * ========================================== 序列化 与反序列化 =====================================================
 */
/**
 * 序列化与反序列 化
 * @param user
 * @return
 */
@PostMapping("edit/user")
public ResponseResult editUser(@RequestBody User user){

    return ResponseResultFactory.success("添加用户",user);
}
对象字段
@NotBlank(message = "邮箱不能我空",groups = {UserUpdate.class})
@JsonSerialize(using = ToUpperCaseSerializer.class)
@JsonDeserialize(using = ToLowerCaseCaseSerializer.class)
private String email;
2.7 日期格式 局部处理
/**
 * 日期格式参数绑定
 * @param date
 * @return
 */
@PostMapping("edit/date")
public ResponseResult editDate(Date date){
    return ResponseResultFactory.success("添加用户",date);
}

/**
 * 局部使用  类似 注解在对象上 @DateTimeFormat(pattern = "yyyy/MM/dd HH:ss")   //入参 spring 注解 格式化时间 可参考全局配置
 * @param binder
 */
@InitBinder
public void initBinder(WebDataBinder binder){
    binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
}

对象处理 可以使用注解

springboot 自定义HandlerExecutionChain springboot自定义参数解析_添加用户_03

 

全局配置:

springboot 自定义HandlerExecutionChain springboot自定义参数解析_添加用户_04

 

3.0 下面就是自定义参数解析器了,也就是 为什么参数 会根据类型 自动解析出绑定对应的数据 其实原理就是折磨简单 具体实现 如下
/**
 * 自定义 解析器
 * @param user
 * @return
 */
@PostMapping("edit/users2")
public ResponseResult editUser2(@CurrentUser String user){
    return ResponseResultFactory.success("添加用户",user);
}
@Component
public class CurrentUserArgumentResolver implements HandlerMethodArgumentResolver {
    /**
     * 适配有对应注解方法的参数 true 这回走下面的解析
     * @param methodParameter
     * @return
     */
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.hasParameterAnnotation(CurrentUser.class);
    }

    /**
     * 线上可以 考虑 从session 或者 redis ,mysql 等 容器获取当前用户信息
     * @param methodParameter
     * @param modelAndViewContainer
     * @param nativeWebRequest
     * @param webDataBinderFactory
     * @return
     * @throws Exception
     */
    @Override
    public Object resolveArgument(MethodParameter methodParameter, @Nullable ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, @Nullable WebDataBinderFactory webDataBinderFactory) throws Exception {
        return "helloWorld";
    }

 

 


elk