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

相关推荐
写代码的【黑咖啡】11 分钟前
Python 中的 Requests 库:轻松进行 HTTP 请求
开发语言·python·http
BD_Marathon11 分钟前
MyBatis各种查询功能
java·开发语言·mybatis
研☆香13 分钟前
JavaScript 特点介绍
开发语言·javascript·ecmascript
Howrun77715 分钟前
虚幻引擎_AController_APlayerController_AAIController
开发语言·c++·游戏引擎·虚幻
雪人.17 分钟前
Spring常见面试题(2026版30道面试题)
java·后端·spring
温暖小土19 分钟前
深入理解 Spring Boot 配置加载顺序:外部化配置的艺术
java·springboot
曹牧23 分钟前
C#:ToDouble
开发语言·c#
小林rr23 分钟前
深入探索 C++:现代特性、工程实践与性能优化全解
java·c++·性能优化
袁袁袁袁满25 分钟前
Python读取doc文件打印内容
开发语言·python·python读取doc文件
zcfeng53037 分钟前
PHP升级
开发语言·php