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

相关推荐
hixiong1236 分钟前
C# 使用 ZXingCpp.Angle 进行条码、二维码识别
开发语言·c#·扫码
云和数据.ChenGuang7 分钟前
AI 工程师相关就业岗位(细分,偏工程落地,由底层到上层)
服务器·开发语言·人工智能·python
爱读书的小胖17 分钟前
Codewars:Last digit of a large number(C)解题分析
c语言·开发语言·r语言
企业数字化笔记18 分钟前
租户停用后数据和任务怎么处理?冻结、恢复与保留策略的实现
android·java·spring boot
CarIise25 分钟前
Spring MVC 请求参数绑定与常用注解
java·spring·mvc
码途行者30 分钟前
处理超时未支付订单:两套轻量化方案,附 PHP 极简实现
开发语言·redis·php
云和数据.ChenGuang34 分钟前
LangChain 实战指南:从入门到构建智能应用
开发语言·人工智能·python·langchain
const_qiu36 分钟前
第三章 04:3.6 基础调用链的构建
开发语言·python
一条泥憨鱼37 分钟前
AI Agent 安全实践:从 MCP 工具调用到 Java 权限校验
java·人工智能·安全
向上的车轮38 分钟前
Python 大数据处理不慢:Polars + DuckDB + Arrow 实战指南
开发语言·python