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

相关推荐
许彰午39 分钟前
03-三种开发模式
java·架构
felixking44 分钟前
C++20 协程
开发语言·c++·协程
Rain的Java大神之路1 小时前
短信接口被狂刷怎么处理
java·运维·后端·web安全·面试·架构·xss
2602_959960922 小时前
谢飞机面试大厂:Spring Boot、JVM、Redis、Kafka、微服务与音视频搜索场景求生实录
java·jvm·spring boot·redis·面试题
Zane1994122 小时前
CAS 与原子类:Java 如何实现无锁编程
java·开发语言
略略略咯咯2 小时前
AI玩具-study
java
码农颜3 小时前
6.3 主函数流程剖析
开发语言·php
TheBestRucy3 小时前
Python 九阳神功:从零筑基到线程飞升
服务器·开发语言·网络·人工智能·python
研☆香3 小时前
聊一聊前端的常见字 关键字
开发语言·前端·javascript
xier_ran4 小时前
【infra之路】GPU 存储层次总结:L1 / L2 / HBM
java·网络·数据库