Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring RestController returning error as html instead of json

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?

like image 881
user3565529 Avatar asked May 19 '18 03:05

user3565529


People also ask

What is the return type of RestController?

1.2. @RestController. As name suggest, it shall be used in case of REST style controllers i.e. handler methods shall return the JSON/XML response directly to client rather using view resolvers. It is a convenience annotation that is itself annotated with @Controller and @ResponseBody .

Can we use controller instead of RestController?

@Controller is used to mark classes as Spring MVC Controller. @RestController annotation is a special controller used in RESTful Web services, and it's the combination of @Controller and @ResponseBody annotation. It is a specialized version of @Component annotation.

What is difference between controller and RestController annotation?

The @Controller annotation indicates that the class is controller like web Controller while @RestController annotation indicates that the class is controller where @RequestMapping Method assume @ResponseBody by Default(i.e REST APIs).


1 Answers

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.

like image 59
Muhammad Hassnain Avatar answered Sep 29 '22 16:09

Muhammad Hassnain