zoukankan      html  css  js  c++  java
  • 一、自定义参数解析

    在使用Spring MVC传递List参数时直接自动映射出错如下

        @ResponseBody
        @PostMapping("/clarifySumitting")
        public String clarifySumitting(ArrayList<ClarifyIssue> clarifyIssues) {
            
            return "true";
        }

    JQuery提交代码

    function form_submitting(selections){
    	//此处暂时禁用表格
    	$table.bootstrapTable('showLoading');
    	
    	$.post('clarifySumitting',{"clarifyIssues":JSON.stringify(selections)},function(response){
    		
    	});
    	
    	//取消禁用
    	$table.bootstrapTable('hideLoading');
    }
    

     无法完成自动映射到Controller

    采用自定义解析器配置完成自动映射

    一、解析处理代码如下

    package com.huaqin.workflow.handler;
    
    import java.lang.reflect.ParameterizedType;
    import java.lang.reflect.Type;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.core.MethodParameter;
    import org.springframework.web.bind.support.WebDataBinderFactory;
    import org.springframework.web.context.request.NativeWebRequest;
    import org.springframework.web.method.support.HandlerMethodArgumentResolver;
    import org.springframework.web.method.support.ModelAndViewContainer;
    
    import com.alibaba.fastjson.JSON;
    
    public class ArrayListArgumentResolver implements HandlerMethodArgumentResolver {
    
        @Override
        public boolean supportsParameter(MethodParameter parameter) {
            return ArrayList.class.isAssignableFrom(parameter.getParameterType());
        }
    
        @Override
        public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
                NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
                /**
                 * 获取 Collection 中的泛型类型进行映射实例化
                 */
                Type[] types = ((ParameterizedType)parameter.getGenericParameterType()).getActualTypeArguments();
                if (types.length==0) {
                    throw new RuntimeException("com.huaqin.workflow.handler.CollectionsArgumentResolver:检测不到泛型映射");
                }
                Class<?> clazz = (Class<?>) types[0];
                
                /**
                 * 通过参数名称获取请求数据
                 */
                List<?> list = JSON.parseArray(webRequest.getParameter(parameter.getParameterName()),clazz);
            return list;
        }
    }

    二、交由Spring MVC处理器处理

    package com.huaqin.workflow.controller;
    
    import java.util.List;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
    
    @Configuration
    public class WebMvcConfg implements WebMvcConfigurer {
    
    	
    	@Override
    	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    		// TODO Auto-generated method stub
    		converters.add(0, new FastJsonHttpMessageConverter());
    		WebMvcConfigurer.super.configureMessageConverters(converters);
    	}
    	
    }
    

    完成自动映射

      

  • 相关阅读:
    IP分片与TCP分片的考虑
    snort-2.9.16在ubuntu16.04环境下的安装,配置与运行
    snort 程序流程图
    宿主机、容器、真实时间不一致问题
    ffi动态链接库的使用
    docker容器中nginx日志的分割
    dockerFile指令详解
    关于如何查看多网卡物理机中网卡序号与物理网卡的对应该关系
    Curl相关参数意义及使用方式
    docker 基础入门
  • 原文地址:https://www.cnblogs.com/black-/p/8967947.html
Copyright © 2011-2022 走看看