请求参数存在以0开头的正整数,报JSON parse error:Invalid numeric value:Leading zeroes not allowed错误

在Spring Boot项目中遇到请求参数以0开头的正整数导致的JSON解析错误。使用Swagger测试接口时,传入以0开头的数字会触发该错误。解决方案是通过配置类来修正这个问题,确保0开头的数字能正确存入数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring boot项目中,请求参数存在以0开头的正整数,报JSON parse error:Invalid numeric value:Leading zeroes not allowed错误,错误提示就是说不允许以0开头

 

用swagger接口测试的时候传的参数值中有以0开头的正整数

 

 

最后的解决方式是添加一个配置类来解决,然后传的参数值05将自动被转换为5存入数据库

 

 


附上配置类完整代码

package com.citms.config;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;


@Configuration
public class WebParameterConfig extends WebMvcConfigurerAdapter {
 
	/**
	 * 替换框架json为fastjson
	 * @param converters
	 */
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
 
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
 
		//驼峰转下划线
		SerializeConfig serializeConfig=new SerializeConfig();
		serializeConfig.propertyNamingStrategy= PropertyNamingStrategy.SnakeCase;
		fastJsonConfig.setSerializeConfig(serializeConfig);
		//序列化格式
		fastJsonConfig.setSerializerFeatures(
				SerializerFeature.PrettyFormat,
				SerializerFeature.WriteNullStringAsEmpty
		);
		// 处理中文乱码问题
		List<MediaType> fastMediaTypes = new ArrayList<>();
		fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
		fastConverter.setSupportedMediaTypes(fastMediaTypes);
		fastConverter.setFastJsonConfig(fastJsonConfig);
 
		//处理字符串, 避免直接返回字符串的时候被添加了引号
		StringHttpMessageConverter smc = new StringHttpMessageConverter(Charset.forName("UTF-8"));
		converters.add(smc);

		converters.add(fastConverter);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值