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);
        }
    }
    
  • 相关阅读:
    c++ malloc函数
    CString,string,char*之间的转换
    CString & 类型和引用
    权限管理页面管理
    权限管理模块管理
    Infor SyteLine 8.02 DataBase Server Install(Summary)
    SyteLine在SQL Server启用CLR
    AMD(马来西亚槟城)
    Infor SyteLine 8.02 Utility Server SyteLine Install
    Infor SyteLine 8.02 Utility Server Install(Summary)
  • 原文地址:https://www.cnblogs.com/liujinming/p/15547463.html
Copyright © 2011-2022 走看看