Spring ResponsEntity body contains extra json with timestamp, status and more
Spring ResponsEntity body contains extra json with timestamp, status and more
We have an REST endpoint that will add a new empty ingredient to an existing meal:
@RequestMapping(value = "/add", method = RequestMethod.PUT, consumes = "application/json;charset=UTF-8")
public ResponseEntity<Object> add(@RequestBody final Meal meal) throws URISyntaxException
Optional<Meal> optionalMeal = mealRepository.findById(meal.getId());
if (!optionalMeal.isPresent())
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(MessageUtil.parse(MSG_404_MEAL, meal.getId() + ""));
Ingredient ingredient = new Ingredient();
ingredient.setMeal(optionalMeal.get());
ingredientRepository.saveAndFlush(ingredient);
ResponseEntity re = ResponseEntity
.created(RequestUtil.getResourceURI(ingredient.getId()))
.body(ingredient);
return re;
Ingredient is an entity class with some fields:
public class Ingredient implements Serializable
@Id
private Integer id;
private Meal meal;
private Grocery grocery;
private Float amount;
...
RequestUtil takes care of creating the URI where the newly created resource is to be found:
public class RequestUtil
public static URI getResourceURI(int id) throws URISyntaxException
final String url = RequestUtil.getCurrentRequest().getRequestURL().toString();
final String req = RequestUtil.omitLast(url);
return new URI(req + "get/" + id);
public static HttpServletRequest getCurrentRequest()
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
return ((ServletRequestAttributes) requestAttributes).getRequest();
public static String omitLast(final String url)
return url.substring(0, url.lastIndexOf("/") + 1);
The http status code and resource URI end up correctly in the response headers, but the body contains two JSONs:
"id": 407,
"meal":
"id": 99,
"name": "New Meal",
"active": true
,
"grocery": null,
"amount": null,
"bought": false
"timestamp": "2018-08-29T19:25:31.466+0000",
"status": 201,
"error": "Created",
"message": "No message available",
"path": "/ingredient/add"
Our javascript code does not expect this extra data and fails with
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 114 of the JSON data
Using a debugger, we can see that by the time the code reaches the return statement in add(), the ResponseEntity does not contain this extra data. Can someone explain where it comes from, and how we stop it from polluting the response?
Thanks for any help!
Any chance that there is an extra servlet filter configured which adds extra JSON to the response in case it sees a 201 status code ?
– Clemens Klein-Robbenhaar
Aug 29 '18 at 21:45
@lealceldeiro That's exactly the problem. The two concatenated JSONs are directly copied from the response as seen in my browser's Network tab.
– Mr. Wrong
Aug 30 '18 at 5:52
@ClemensKlein-Robbenhaar The project is a spring boot application, so there's Spring, but nothing else.
– Mr. Wrong
Aug 30 '18 at 8:14
With spring boot there is still the usual java webapp stack, including servlet filters. spring boot might actually add a bunch of them in its default configuration
– Clemens Klein-Robbenhaar
Sep 16 '18 at 20:26
0
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
Please, take a look at the Network tab in the devtools and post the exact response body. The code posted below the text but the body contains two JSONs text is not a valid JSON
– lealceldeiro
Aug 29 '18 at 20:36