Spring Error Response auto converted to Html from

2019-08-02 10:28发布

I am getting error response correctly when I am running the project in my end.

{
 "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"
 }

But when I deploy on server It automatically converts to HTML. How to fix this?

<!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>

3条回答
我欲成王,谁敢阻挡
2楼-- · 2019-08-02 11:04

i think it may helps ,

@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");
              }
}

here for JSON data response we need to use produces="application/json". and better to use Response Entity instead of @ResponseBody . noting is special but Response Entity can carry response with Http Status Codes.

if it helps please promote.

查看更多
ら.Afraid
3楼-- · 2019-08-02 11:12

That looks like your server (Tomcat) or your Web Application has a default 500 error page that is being applied.

查看更多
Luminary・发光体
4楼-- · 2019-08-02 11:14

This is fixed by creating a default error page such that the tomcat does not direct to error HTML page

查看更多
登录 后发表回答