【发布时间】:2021-03-20 16:06:09
【问题描述】:
现在我正在研究spring-boot。
所以我创建了一个视图,并在此页面中通过 ajax 发送请求。
以下代码是在jsp中的javascript上。
fetch("/login/login.ajax",{
data:{
"test":"a"
}
}).then((res)=>{
return res.json();
}).then((res)=>{
console.log(res);
})
下面的代码是在一个 spring-boot 控制器上
@RequestMapping("/login/login.ajax")
@ResponseBody
public HashMap<String, Object> login(@RequestBody JSONObject request){
HashMap<String, Object> resultMap = new HashMap<String, Object>();
if(request != null){
Object req = request.get("test");
System.out.println(req.toString());
if(req.toString().equals("a")){
resultMap.put("data","cool");
}
else{
resultMap.put("data","hot");
}
log.info("ajax-login");
}
else{
resultMap.put("errorCode", "0001");
}
return resultMap;
}
现在 我在 chrome 的控制台上收到此错误。
时间戳:“2020-12-09T09:22:27.095+0000”,状态:400,错误:“错误请求”,消息:“缺少必需的请求正文:public java.util….login.LoginCtr.login (org.json.simple.JSONObject)”,跟踪:“org.springframework.http.converter.HttpMessageNotR…java.base/java.lang.Thread.run(Thread.java:832) ↵", …} 错误:“错误请求” 消息:“缺少所需的请求正文:public java.util.HashMap
toast.test.login.LoginCtr.login(org.json.simple.JSONObject)” 路径:“/login/login.ajax” 状态:400 时间戳:“2020-12-09T09:22:27.095+0000” 跟踪:“org.springframework.http.converter.HttpMessageNotReadableException:缺少所需的请求正文:public java.util.HashMap toast.test.login.LoginCtr.login(org. json.simple.JSONObject) ↵ 在 org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:161) ↵ 在 org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:131) ↵ 在 org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ...
好吧,我认为 fetch 出错是可以的,因为它不合适。
但此错误在客户端获取有关服务器端的详细信息。
我想隐藏所有这些错误,尤其是关于消息和跟踪的错误。
我该怎么做?请给我一些建议。
【问题讨论】:
标签: java spring spring-boot spring-mvc