Could not convert JavaScript argument

本文描述了在使用火狐浏览器进行测试时遇到的一个JavaScript异常问题,并详细记录了解决该问题的过程。问题源于向$.getJSON方法传递了一个错误类型的参数,即String数组而非预期的JSON格式。

[09:30:59.154] uncaught exception: [Exception... "Could not convert JavaScript argument"  nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)"  location: "JS frame :: http://localhost:8086/XXXXX/common/js/jquery/jquery-1.6.4.min.js :: <TOP_LEVEL> :: line 4"  data: no]

火狐测试时出现如上错误,查了一些相关的,都没解决。

后来发现发送json时的参数传错了,传成String数组型了,导致jquery无法解析。

如下:

var transCodes = document.getElementsByName("transCode");

$.getJSON("/XXX/XXXcs?m=<%=r%>",{Id:$("#id").val(),transCodes: transCodess},

把transCodes作为参数发送了。

<think>我们正在讨论Spring框架中类型转换的错误。用户引用了多个类似错误,核心问题是Spring无法将提供的值转换为目标类型(如double、Date等)。错误信息通常包含:"Could not convert argument value of type [X] to required type [Y]"。 用户提供的引用中: - [^1]:将null赋值给基本类型double导致的异常(基本类型不能为null)。 - [^2]:构造方法注入时,将字符串"1998-9-9"注入到Date类型属性失败(Spring默认不支持字符串到Date的转换)。 - [^3]:将字符串类型转换为SqlSessionFactory类型失败(类型不匹配)。 - [^4]:SpringMVC中字符串到Date的转换问题,并提到了CustomDateEditor的使用。 用户的问题是:查找关于'Could not convert argument value of type []'错误的解决方案。 解决方案的核心在于:Spring的类型转换机制。Spring使用PropertyEditor或Converter进行类型转换。当遇到无法转换的类型时,我们需要注册自定义的类型转换器。 以下是针对不同情况的解决方案: 1. **基本类型(如double)不能为null**:如果目标属性是基本类型(如double、int等),则不能注入null值。解决方案: - 将基本类型改为对应的包装类型(如Double、Integer),这样就能接受null值。 - 或者在注入时确保提供的值不为null。 2. **日期类型转换**:Spring默认不支持将字符串自动转换为java.util.Date。解决方案: - 使用自定义属性编辑器(PropertyEditor): 在Spring配置文件中注册一个自定义的PropertyEditor,例如: ```xml <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="java.util.Date" value="org.springframework.beans.propertyeditors.CustomDateEditor"> <bean class="org.springframework.beans.propertyeditors.CustomDateEditor"> <constructor-arg> <bean class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd"/> </bean> </constructor-arg> <constructor-arg value="true"/> <!-- 允许空字符串 --> </bean> </entry> </map> </property> </bean> ``` 或者使用注解方式(在Controller中): ```java @InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); } ``` 3. **自定义类型转换**:对于其他自定义类型(如将字符串转换为SqlSessionFactory),需要自定义转换器。可以通过实现`Converter`接口或使用`GenericConverter`: - 实现`Converter<S, T>`接口(例如:`Converter<String, SqlSessionFactory>`),然后在Spring中注册该转换器。 - 注册方式(使用Java配置): ```java @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addFormatters(FormatterRegistry registry) { registry.addConverter(new StringToSqlSessionFactoryConverter()); } } ``` 其中,`StringToSqlSessionFactoryConverter`是自定义的转换器类。 4. **检查注入的值和目标类型是否匹配**:确保在XML配置或注解中提供的值能够被转换为目标类型。例如,引用[^3]中,错误地将一个字符串值注入到需要SqlSessionFactory类型的参数中,这显然不匹配。 总结步骤: - 步骤1:检查目标属性类型。如果是基本类型,考虑改为包装类型以避免null值问题。 - 步骤2:对于日期类型,注册自定义属性编辑器(PropertyEditor)或转换器(Converter)。 - 步骤3:对于其他自定义类型,实现并注册转换器。 - 步骤4:确保注入的值格式正确,且与目标类型匹配。 注意事项: - 在XML配置中,使用`<constructor-arg>`或`<property>`时,Spring会尝试将字符串值转换为属性或参数声明的类型。如果转换失败,就会报错。 - 使用注解配置(如@Value)时,同样要注意类型转换。 示例(针对引用[^2]中的日期转换问题): 在Spring配置文件中注册CustomDateEditor后,就可以将字符串转换为Date类型: ```xml <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="java.util.Date" value="org.springframework.beans.propertyeditors.CustomDateEditor"> <bean class="org.springframework.beans.propertyeditors.CustomDateEditor"> <constructor-arg> <bean class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd"/> </bean> </constructor-arg> <constructor-arg value="true"/> </bean> </entry> </map> </property> </bean> ``` 这样,在构造方法注入时,Spring就能正确地将字符串"1998-9-9"转换为Date对象。 </think>以下是针对"Could not convert argument value of type []"错误的完整解决方案,结合Spring框架的类型转换机制: ### 核心问题分析 该错误表明Spring框架无法将提供的参数值(通常是字符串或null)转换为目标类型(如`double`、`Date`或自定义类型)。主要发生在依赖注入(构造方法或setter注入)时[^1][^2][^4]。 ### 解决方案步骤 1. **处理基本类型null值问题** - 错误:`Failed to convert property value of type 'null' to required type 'double'` - 原因:基本类型(如double/int)不能接受null值 - 修复方案: ```xml <!-- 将基本类型改为包装类型 --> <constructor-arg name="bonus" value="0.0"/> <!-- 提供默认值 --> 或 <!-- 修改目标属性类型为包装类 --> private Double bonus; // 代替double ``` 2. **日期类型转换方案** - 错误:`Cannot convert value of type [java.lang.String] to required type [java.util.Date]` - 方案1:注册自定义属性编辑器 ```xml <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="java.util.Date" value="org.springframework.beans.propertyeditors.CustomDateEditor"> <bean class="java.text.SimpleDateFormat" factory-method="getDateInstance"> <constructor-arg value="yyyy-MM-dd"/> </bean> <bean class="java.lang.Boolean" factory-method="valueOf"> <constructor-arg value="true"/> <!-- 允许空值 --> </bean> </entry> </map> </property> </bean> ``` [^4] - 方案2:使用转换服务(推荐) ```java @Configuration public class AppConfig implements WebMvcConfigurer { @Override public void addFormatters(FormatterRegistry registry) { DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar(); registrar.setUseIsoFormat(true); registrar.registerFormatters(registry); } } ``` 3. **自定义类型转换** - 错误:`Cannot convert value of type 'java.lang.String' to required type 'SqlSessionFactory'` - 实现`Converter`接口: ```java public class StringToSqlSessionFactoryConverter implements Converter<String, SqlSessionFactory> { @Override public SqlSessionFactory convert(String source) { // 实现自定义转换逻辑 return new SqlSessionFactoryBuilder().build(/*配置*/); } } ``` - 注册转换器: ```xml <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="com.example.StringToSqlSessionFactoryConverter"/> </set> </property> </bean> ``` [^3] 4. **防御性编程技巧** - 添加空值检查: ```java public MyClass(@Value("${date.param:#{null}}") Date dateParam) { if(dateParam == null) { throw new IllegalArgumentException("日期参数不能为空"); } } ``` - 使用Optional包装: ```java public MyClass(Optional<Double> bonus) { this.bonus = bonus.orElse(0.0); } ``` ### 最佳实践建议 1. **优先使用包装类型**:用`Double`代替`double`,`Integer`代替`int`等 2. **明确格式规范**:日期/时间值统一使用ISO格式(如`yyyy-MM-dd'T'HH:mm:ss`) 3. **单元测试验证**: ```java @Test public void testDateConversion() { String dateStr = "2023-05-15"; Date date = conversionService.convert(dateStr, Date.class); assertNotNull(date); } ``` ### 常见错误场景排查表 | 错误现象 | 根本原因 | 解决方案 | |----------|----------|----------| | `null → double` | 基本类型不接受null | 改用Double包装类型 | | `String → Date` | 无默认转换器 | 注册CustomDateEditor | | `String → CustomType` | 无转换逻辑 | 实现Converter接口 | | 空字符串报错 | 未启用空值处理 | 设置`allowEmpty=true`[^4] | | 格式不匹配 | 日期格式不一致 | 统一日期格式规范 | > **关键提示**:Spring的类型转换系统基于`PropertyEditor`(旧版)和`Converter`(新版)实现。对于复杂转换需求,推荐使用`org.springframework.core.convert.converter.Converter`接口[^3][^4]。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值