4

My spring boot application returning below json response when controller responds with 400-BadRequest.

{
  "readyState": 4,
  "responseText": "{\r\n  \"success\" : false,\r\n  \"projects\" :null,\r\n  \"httpStatusCode\" : \"400\",\r\n  \"message\" : \"Request  Processing failed.\",\r\n  \"requestErrors\" : [ {\r\n    \"error\" : \"platform required.\"\r\n  }, {\r\n    \"error\" : \"id required.\"\r\n  }, {\r\n    \"error\" : \"name required.\"\r\n  } ]\r\n}",
"responseJSON": {
"success": false,
"projects": null,
"httpStatusCode": "400",
"message": "Request Processing failed.",
"requestErrors": [
  {
    "error": "platform required."
  },
  {
    "error": "id required."
  },
  {
    "error": "name required."
  }
]
},
 "status": 400,
 "statusText": "Bad Request" 
}

But here i dont want to see json elements responseText, status, and statusText, since my JSON object itself having these information.

Here is my CustomErrorAttributes class.

public class CustomErrorAttribute implements ErrorAttributes {
@Override
public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {
    Map<String, Object> errorAttributes = new LinkedHashMap<String, Object>();
    errorAttributes.put("timestamp", new Date());
    return errorAttributes;
}

@Override
public Throwable getError(RequestAttributes requestAttributes) {
    return new IllegalStateException("Illegal State of the request.");
}
}

Java Config:

@Bean
public ErrorAttributes customErrorAttribute(){
    return new CustomErrorAttribute();
}

I tried with registering custom implementation of ErrorAttributes as @Bean as specified here

But no luck, please any help. Thanks.

Community
  • 1
  • 1
Lovababu Padala
  • 2,415
  • 2
  • 20
  • 28

1 Answers1

3

You need to make Spring aware of your CustomErrorAttribute implementation.

Just add a configuration class (or simply add the @Bean part to one of your existing Spring configuration classes) like:

@Configuration
public class MvcErrorConfig {

    @Bean
    public CustomErrorAttribute errorAttributes() {
        return new CustomErrorAttribute();
    }

}

and everything works out of the box.

This part of the documentation has all the details. The relevant Spring Boot autoconfiguration class is ErrorMvcAutoConfiguration

geoand
  • 60,071
  • 24
  • 172
  • 190
  • I am sure `CustomErrorAttribute` is Spring aware, since i am able to autowire it. githug link to my project https://github.com/lovababu/SpringBootExample – Lovababu Padala Jun 15 '15 at 12:34
  • Ok, I run the project and get back to you – geoand Jun 15 '15 at 12:36
  • The error handling in your project is working out of the box for me. The `CustomErrorAttribute` class is being used correctly by Spring. Perhaps you should perform a `gradle clean` and see what happens – geoand Jun 15 '15 at 12:59
  • I should also mention that the code worked when I ran the main class via the IDE, or using `Gradle`. – geoand Jun 15 '15 at 13:03
  • You meant to say, are you getting response without those json element aforementioned? I did clean and ran the main through IDE, but no luck still same response. – Lovababu Padala Jun 15 '15 at 13:08
  • Yes, the only attribute present in the response was the timestamp, nothing else. Actually, I only tested the HTTP 404 scenario. I haven't checked the HTTP 400 scenario. I'll do that now – geoand Jun 15 '15 at 13:12
  • Ok, I tried posting `{}` to `/projects`, and what I got in response was: `{ "success": false, "projects": null, "httpStatusCode": "400", "message": "Request Processing failed.", "requestErrors":[ { "error": "platform required." }, { "error": "name required." }, { "error": "description required." }, { "error": "id required." } ] }` – geoand Jun 15 '15 at 13:16
  • Sorry @geoand it seems [jsondoc](http://jsondoc.org/) is miss lead me. when i sent request through chrome postman i see expected response, but not with jsondoc ui. Thanks for your help. – Lovababu Padala Jun 15 '15 at 13:27