在处理项目中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解决!!