解决Field error in object ‘user‘ on field ‘name‘: rejected value [Tom]问题

问题描述

使用postman传递参数,报错400。

400 Bad Request错误的含义

400 Bad Request错误表示服务器无法理解客户端发送的请求,原因通常是由于客户端发送的请求存在问题。

完整的报错信息如下:

复制代码
Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors<EOL>Field error in object 'user' on field 'name': rejected value [Tom]; codes [typeMismatch.user.name,typeMismatch.name,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.name,name]; arguments []; default message [name]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'name'; nested exception is java.lang.NumberFormatException: For input string: "Tom"]]

问题分析

报错400说明客户端传递参数无法正确被服务器接收,报错的重点:

  1. rejected value [Tom] ; codes [typeMismatch.user.name,typeMismatch.name,typeMismatch.int,typeMismatch]
  2. Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'name'

类型不匹配,name要求int类型,服务器不会认为是自己设置有问题,会判定是客户端传递参数有问题。

回到idea找到代码,果然是int类型。

java 复制代码
private int name;

int 类型是idea联想后直接tab生成出来的,解决方式就是把name换成String类型,重新生成getter和setter,问题解决。

相关推荐
二哈赛车手1 小时前
新人笔记---ApiFox的一些常见使用出错
java·笔记·spring
为何创造硅基生物2 小时前
C语言 结构体内存对齐规则(通俗易懂版)
c语言·开发语言
吃好睡好便好2 小时前
在Matlab中绘制横直方图
开发语言·学习·算法·matlab
栗子~~2 小时前
JAVA - 二层缓存设计(本地缓冲+redis缓冲+广播所有本地缓冲失效) demo
java·redis·缓存
星寂樱易李2 小时前
iperf3 + Python-- 网络带宽、网速、网络稳定性
开发语言·网络·python
YDS8292 小时前
DeepSeek RAG&MCP + Agent智能体项目 —— RAG知识库的搭建和接口实现
java·ai·springboot·agent·rag·deepseek
仰泳之鹅2 小时前
【C语言】自定义数据类型2——联合体与枚举
c语言·开发语言·算法
之歆2 小时前
DAY_12JavaScript DOM 完全指南(二):实战与性能篇
开发语言·前端·javascript·ecmascript
未若君雅裁3 小时前
MyBatis 一级缓存、二级缓存与清理机制
java·缓存·mybatis
cen__y3 小时前
Linux12(Git01)
linux·运维·服务器·c语言·开发语言·git