12

I have a spring boot webapp in which I have defined REST APIs. I am using spring security for REST API security.

I have annotated my controller classes with RestController. I recently updated spring boot, mvc and security to the latest version.

I am now seeing that in my negative cases, before the update it was returning json error response, but now after the update, it is returning html error response.

Before the update, it was giving following error response-

{
  "timestamp": "2018-05-21T18:22:37.105+0000",
  "status": 500,
  "error": "Internal Server Error",
  "message": "Error message.",
  "path": "<API path>"
}

After the update, it is giving following response.

<!DOCTYPE html>
<html>
    <head>
        <title>Apache Tomcat/8.0.51 - Error report</title>
        <style type="text/css">H1 {font-family:Tahoma,Arial,sansserif;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 - Error message.</h1>
        <div class="line"></div>
        <p>
            <b>type</b> Status report
        </p>
        <p>
            <b>message</b>
            <u>Error message.</u>
        </p>
        <p>
            <b>description</b>
            <u>The server encountered an internal error that prevented it from fulfilling this request.</u>
        </p>
        <hr class="line">
        <h3>Apache Tomcat/8.0.51</h3>
    </body>
</html>

I did not had any ExceptionHandler in my Controller. It was coming as json error by default. I have jackson and jackson-mapper-asl in my libs.

I could not figure out if there is a default setting somewhere that needs to be changed to send errors as json.

I also tried to disable ErrorPageFilter but even after that I am getting HTML response.

Is there any way by which I can get json response rather than html?

7
  • Does your mvc configuration contain a message converter for JSON? Commented May 19, 2018 at 4:10
  • 1
    Could you give more details, like HTML error message, expected JSON error message, and RestController values. Commented May 19, 2018 at 6:46
  • Updated the description with the sample responses Commented May 21, 2018 at 18:26
  • I dont have a configuration for message converter. Commented May 21, 2018 at 18:27
  • But I have jackson in my libs Commented May 21, 2018 at 18:32

1 Answer 1

1

I think you are missing the kind of response controller will produce.

Add this to your endpoint:

@ResponseBody
@RequestMapping(value="/xyz", produces="application/json")
public String XYZ() {
    // Your Code Goes Here
    return  return new JSONObject("{'aa':'bb'}");
}

Check these posts: Returning JSON response from spring controller goes as html instead of JSON in javascript and Returning JSON object as response in Spring Boot.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.