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);
        }
    }
    
  • 相关阅读:
    delphi 的插件机制与自动更新
    delphi 的 ORM 框架
    canner CMS 系统 (公司在台湾) https://www.canner.io/
    区块链 ---- 数字货币交易
    BIM平台 http://gzcd.bim001.cn
    TreeGrid 控件集 :delphi 学习群 ---- 166637277 (Delphi学习交流与分享)
    UniGUI 如何进行 UniDBGrid 的单元 Cell 的计算 ?
    国产 WEB UI 框架 (收费)-- Quick UI,Mini UI
    iOS尽量不要在viewWillDisappear:方法中移除通知
    Tips:取消UICollectionView的隐式动画
  • 原文地址:https://www.cnblogs.com/liujinming/p/15547463.html
Copyright © 2011-2022 走看看