一、自定义参数解析

在使用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);
	}
	
}

完成自动映射

  

 

转载于:https://www.cnblogs.com/black-/p/8967947.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值