【问题标题】:Spring Error Response auto converted to Html from JSONSpring 错误响应从 JSON 自动转换为 Html
【发布时间】:2018-05-30 10:24:44
【问题描述】:

当我最终运行项目时,我得到了正确的错误响应。

{
 "timestamp": "2018-05-30T10:16:00.065+0000",
 "status": 500,
 "error": "Internal Server Error",
 "message": "No login found for username :string",
 "path": "/gatepass/api/v1/logins/login"
 }

但是当我在服务器上部署时它会自动转换为 HTML。如何解决这个问题?

<!doctype html>
<html lang="en">
   <head>
      <title>HTTP Status 500 – Internal Server Error</title>
      <style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style>
   </head>
   <body>
      <h1>HTTP Status 500 – Internal Server Error</h1>
      <hr class="line" />
      <p><b>Type</b> Status Report</p>
      <p><b>Message</b> No login found for username :string</p>
      <p><b>Description</b> The server encountered an unexpected condition that prevented it from fulfilling the request.</p>
      <hr class="line" />
      <h3>Apache Tomcat/8.5.27</h3>
   </body>
</html>

【问题讨论】:

  • 这是相同的响应。不同之处在于 Web 容器将响应写入浏览器并因此显示 HTML 标记。您可以将 RequestMapping 中的“produces”选项设置为“application/json”
  • 我已经做到了。但我仍然得到 html 格式的响应
  • 您能分享您发送响应的代码吗?还有其他相关代码sn-ps?

标签: spring-mvc spring-boot


【解决方案1】:

看起来您的服务器 (Tomcat) 或您的 Web 应用程序有一个正在应用的默认 500 错误页面。

【讨论】:

  • 我使用的是tomcat v8.5.27。而且我不想重定向到该页面。我需要 json 格式的错误而不是 html。有没有办法做到这一点?
  • 我是说我认为您已将 Tomcat 或您的应用程序设置为使用默认错误页面。所以首先,我想看看是不是这样。如果是这样,将其更改为不这样做将是一个 Tomcat 问题。并且可能会破坏您希望它也是这样的其他一些原因。但是,您确定要在这种情况下出现 500 错误吗?见codetinkerer.com/2015/12/04/choosing-an-http-status-code.html
  • 嘿,谢谢。我想我不应该发送 500 状态。 500 重定向到导致错误的默认错误页面。但这在创建默认错误页面后得到了解决。但更好的解决方案是在异常中附加适当的状态。再次感谢。
【解决方案2】:

我认为这可能会有所帮助,

@RequestMapping(value = "/userAuthenticationDemo",method = RequestMethod.GET , produces="application/json")
  public ResponseEntity<?> userAuthenticationDemo(@RequestParam("username") String username, @RequestParam("password") String password, ) {

          System.out.println("Authentication hit");

              JSONObject json = new JSONObject();

              json.put("username", username);
              json.put("password", password);

JSONObject authRespObj = gstcService.userAuthenticationForPostalAppChecking(json);

              if(authRespObj != null){
                  authRespObj.put("isErrorFlag", false);
                  return new ResponseEntity<>(authRespObj, new HttpHeaders(), HttpStatus.OK);
              }else{

                  throw new UserException("_USER_AUTHENTICATION_FAILED");
              }
}

对于 JSON 数据响应,我们需要使用 produces="application/json"。 最好使用 Response Entity 而不是 @ResponseBody 。注意是特殊的,但是响应实体可以携带带有 Http 状态码的响应。

如果有帮助,请推广。

【讨论】:

    【解决方案3】:

    通过创建一个默认的错误页面来解决这个问题,这样tomcat不会指向错误的HTML页面

    【讨论】:

    • 你是怎么做到的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    • 2023-04-01
    相关资源
    最近更新 更多