zoukankan      html  css  js  c++  java
  • springmvc封装请求为实体类的时候,将String转化为String[]时效果不理想

    如图所示,寻找解决方案ing

    我最近在看源码,已经看到org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(MethodParameter, ModelAndViewContainer, NativeWebRequest, WebDataBinderFactory)了

    记下来,下次接着看

    看完了,直接上解决方案

    /**
     * 
     */
    package com.grc.common.converter;
    import org.springframework.core.convert.converter.Converter;
    /**
     * @author Lille
     * @time 2021年11月13日 下午4:02:50
     */
    public class StringToStringArrayConverter implements Converter<String,String[]> {
    
    	@Override
    	public String[] convert(String source) {
    		if(source==null) {
    			return new String[0];
    		}else {
    			return new String[] {source};
    		}
    	}
    }
    
    /**
     * 
     */
    package red.lille.makecode.configuration;
    
    import javax.annotation.Resource;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.convert.converter.Converter;
    import org.springframework.format.FormatterRegistry;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    import com.grc.common.converter.StringToStringArrayConverter;
    import com.grc.common.properties.SysConfig;
    import com.grc.common.properties.XMLProperties;
    
    /**
     * @author Lille
     *
     */
    @Configuration
    @EnableWebMvc
    public class XmlConfiguration implements WebMvcConfigurer{
    	@Resource(name = "stringToStringArrayConverter")
    	private Converter<String, String[]> stringToStringArrayConverter;
    
    	@Bean("stringToStringArrayConverter")
    	public StringToStringArrayConverter getStringToStringArrayConverter() {
    		return new StringToStringArrayConverter();
    	}
    	@Override
    	public void addFormatters(FormatterRegistry registry) {
    		registry.addConverter(stringToStringArrayConverter);
        }
    }
    
  • 相关阅读:
    不一样的图片加载方式
    赢 1000 元现金红包!助力奥运,猜金银牌数赢现金
    接入 SDK 结果翻车了?了解 SDK 的那些事
    关于 IPv6 国家有大动作啦!快来瞅瞅行动计划都说了什么~
    MySQL 那些常见的错误设计规范
    webpack 从 0 到 1 构建 vue
    PHP 网络通信底层原理分析
    内部方案汇总
    taro+vue3 引入 taro-ui-vue3
    springboot+tomcat+vue+nginx 前后端分离配置
  • 原文地址:https://www.cnblogs.com/liujinming/p/15547463.html
Copyright © 2011-2022 走看看