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

相关推荐
好吃的肘子几秒前
MongoDB 应用实战
大数据·开发语言·数据库·算法·mongodb·全文检索
ghost1433 分钟前
C#学习第23天:面向对象设计模式
开发语言·学习·设计模式·c#
小白学大数据4 分钟前
Scrapy框架下地图爬虫的进度监控与优化策略
开发语言·爬虫·python·scrapy·数据分析
立秋678910 分钟前
用Python绘制梦幻星空
开发语言·python·pygame
可乐加.糖12 分钟前
项目版本管理和Git分支管理方案
java·git·目标跟踪·gitlab·敏捷流程·源代码管理
明月看潮生30 分钟前
青少年编程与数学 02-019 Rust 编程基础 16课题、包、单元包及模块
开发语言·青少年编程·rust·编程与数学
wowocpp30 分钟前
spring boot Controller 和 RestController 的区别
java·spring boot·后端
后青春期的诗go35 分钟前
基于Rust语言的Rocket框架和Sqlx库开发WebAPI项目记录(二)
开发语言·后端·rust·rocket框架
草莓熊Lotso1 小时前
【C语言字符函数和字符串函数(一)】--字符分类函数,字符转换函数,strlen,strcpy,strcat函数的使用和模拟实现
c语言·开发语言·经验分享·笔记·其他
盛夏绽放1 小时前
Python字符串常用内置函数详解
服务器·开发语言·python