解决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,问题解决。

相关推荐
一只叫煤球的猫3 分钟前
从 JDK1.2 到 JDK21:ThreadLocal的进化解决了什么问题
java·后端·面试
天马行空-14 分钟前
ES 精准匹配 和 模糊查询的实现方式
java·开发语言
Z***258023 分钟前
Java计算机视觉
java·开发语言·计算机视觉
Tiger_shl30 分钟前
SqlConnection、SqlCommand 和 SqlDataAdapter
开发语言·数据库·c#
一点事31 分钟前
ruoyi:集成mybatisplus实现mybatis增强
java·开发语言·mybatis
e***877033 分钟前
Tomcat Request Cookie 丢失问题
java·tomcat·firefox
你的冰西瓜37 分钟前
C++14 新特性详解:相较于 C++11 的主要改进
开发语言·c++·stl
linksinke38 分钟前
Mapstruct引发的 Caused by: java.lang.NumberFormatException: For input string: ““
java·开发语言·exception·mapstruct·numberformat·不能为空
无限进步_1 小时前
C语言单向链表实现详解:从基础操作到完整测试
c语言·开发语言·数据结构·c++·算法·链表·visual studio
likuolei1 小时前
Eclipse 代码模板
java·ide·eclipse