Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringResponseEntity主体包含额外的json,带有时间戳、状态等_Java_Json_Spring - Fatal编程技术网

Java SpringResponseEntity主体包含额外的json,带有时间戳、状态等

Java SpringResponseEntity主体包含额外的json,带有时间戳、状态等,java,json,spring,Java,Json,Spring,我们有一个REST端点,它将向现有膳食添加一种新的空配料: @RequestMapping(value = "/add", method = RequestMethod.PUT, consumes = "application/json;charset=UTF-8") public ResponseEntity<Object> add(@RequestBody final Meal meal) throws URISyntaxException { Optional<M

我们有一个REST端点,它将向现有膳食添加一种新的空配料:

@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;
}
RequestUtil负责创建要在其中找到新创建的资源的URI:

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);
    }
}
http状态代码和资源URI在响应头中正确结束,但正文包含两个JSON:

{
  "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"
}
我们的javascript代码不需要这些额外的数据,因此失败

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 114 of the JSON data
使用调试器,我们可以看到,当代码到达add()中的return语句时,ResponseEntity不包含这些额外的数据。有人能解释它来自哪里,以及我们如何阻止它污染反应吗


谢谢你的帮助

请查看devtools中的Network选项卡,并发布准确的响应正文。在文本下方发布的代码,但正文包含两个JSON文本不是JSONA,任何情况下都不会配置额外的servlet过滤器,在响应中添加额外的JSON,以防它看到201状态代码?@lealcelderio这正是问题所在。这两个连接的JSON直接从响应中复制,如我的浏览器的“网络”选项卡中所示。@ClemensKlein Robbenhaar该项目是一个spring启动应用程序,因此有spring,但没有其他功能。使用spring启动,仍然有常用的java webapp堆栈,包括servlet过滤器。SpringBoot实际上可能会在其默认配置中添加一组
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 114 of the JSON data