【问题标题】:network-communication with xhr(or fetch) and spring-mvc与 xhr(或 fetch)和 spring-mvc 的网络通信
【发布时间】: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


    【解决方案1】:

    您尚未在获取请求中发送正文。也许你可以这样做:

        var data={"test":"a"};
        fetch{url,{
        body: JSON.stringfy(data)
                  }
             }
    

    请阅读 fetch 文档作为参考: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

    【讨论】:

    • 首先感谢您的建议。但我真正想做的是只删除错误消息而不是错误。我认为这个java目录可以在web-browser中看到的错误信息可以在其他情况下显示。
    【解决方案2】:

    谢天谢地,我解决了这个错误。

    我读了这篇文章。 How to suppress Spring boot error message

    下面的代码是我所做的。

    @ExceptionHandler
    @ResponseBody
    public HashMap<String, Object> handlingError(Exception err){
        HashMap<String, Object> result = new HashMap<>();
        result.put("timestamp", Instant.now().toEpochMilli());
        result.put("status",    HttpStatus.METHOD_NOT_ALLOWED.value());
        result.put("error",     HttpStatus.METHOD_NOT_ALLOWED.getReasonPhrase());
        result.put("exception", err.getClass().getName());
        result.put("message", String.format("Request method '%s' not supported", err.getCause()));
        log.info("errorHandler");
        return result;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多