Spring Boot项目误将Integer类型写成int来进行传参

在处理项目中Idea中无报错

问题:

localhost:8080/param/m2在浏览器中输入:localhost:8080/param/m2

产生报错:

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Feb 27 20:55:33 GMT+08:00 2024

There was an unexpected error (type=Internal Server Error, status=500).

Optional int parameter 'id' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type.

原因为:Spring Boot项目误将Integer类型写成int来进行传参

该项目具体的代码为:(错误示范)

复制代码
@RestController
@RequestMapping("/param")
public class ParamController {

    @RequestMapping("/m1")
    public String m1(String name){
        return "这是你的姓名:"+name;
    }

    @RequestMapping("/m2")
    public String m2(String name,int id){  //错误代码,需要将int更改为Integer(包装类)
        return "序列号为:"+ id +"这是你的姓名:"+name;
    }
}

解决方案:

因此,正确的代码为:(正确示范)

复制代码
@RestController
@RequestMapping("/param")
public class ParamController {

    @RequestMapping("/m1")
    public String m1(String name){
        return "这是你的姓名:"+name;
    }

    @RequestMapping("/m2")
    public String m2(String name,Integer id){  
        return "序列号为:"+ id +"这是你的姓名:"+name;
    }
}

重启程序,在浏览器输入:localhost:8080/param/m2?id=5&name=zhangsan

运行结果为:

在Postman中测试为:

Bug解决!!

相关推荐
陌殇殇几秒前
001 Spring AI Alibaba框架整合百炼大模型平台 — 快速入门
人工智能·spring boot·ai
言慢行善14 分钟前
sqlserver模糊查询问题
java·数据库·sqlserver
专吃海绵宝宝菠萝屋的派大星20 分钟前
使用Dify对接自己开发的mcp
java·服务器·前端
大数据新鸟38 分钟前
操作系统之虚拟内存
java·服务器·网络
Tong Z40 分钟前
常见的限流算法和实现原理
java·开发语言
凭君语未可43 分钟前
Java 中的实现类是什么
java·开发语言
He少年1 小时前
【基础知识、Skill、Rules和MCP案例介绍】
java·前端·python
cTz6FE7gA1 小时前
数据结构与算法:大厂面试高频题的深度解析
intellij-idea·perl
克里斯蒂亚诺更新1 小时前
myeclipse的pojie
java·ide·myeclipse