Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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
Spring REST将对象返回到json中,并附加额外的行_Json_Spring_Spring Boot_Spring Mvc_Jackson - Fatal编程技术网

Spring REST将对象返回到json中,并附加额外的行

Spring REST将对象返回到json中,并附加额外的行,json,spring,spring-boot,spring-mvc,jackson,Json,Spring,Spring Boot,Spring Mvc,Jackson,我一直试图返回一个JSON对象,并附加一个额外的值 现在,这段代码返回一个对象的json @GetMapping("/getProduct") public Product getProduct(@RequestParam String productId){ return productService.getById(productId); } 它返回: { "productId": "1",

我一直试图返回一个JSON对象,并附加一个额外的值

现在,这段代码返回一个对象的json

@GetMapping("/getProduct")
    public Product getProduct(@RequestParam String productId){
        return productService.getById(productId);
    }
它返回:

{
  "productId": "1",
  "productName": "product1",
  "productPrice": 100.0,
  "imageLink": null
}
{
  "productId": "1",
  "productName": "product1",
  "productPrice": 100.0,
  "imageLink": null
  "customVariable": somethingsomething
}
但我想添加一个额外的行,以便它返回:

{
  "productId": "1",
  "productName": "product1",
  "productPrice": 100.0,
  "imageLink": null
}
{
  "productId": "1",
  "productName": "product1",
  "productPrice": 100.0,
  "imageLink": null
  "customVariable": somethingsomething
}

我能找到的所有示例要么只返回对象,要么从头构建json。有没有办法将变量附加到对象中?

1-您可以将
自定义变量
字段添加到
产品
实体中

公共类产品{
私有字符串productId;
私有字符串产品名称;
私人双产品价格;
私有字符串图像链接;
私有字符串自定义变量;
公共产品(){
}
//getter/setter。。
}
2-如果要在不修改
产品
实体的情况下添加额外字段:

@GetMapping(“/getProduct”)
公共字符串getProduct(@RequestParam String productId){
Product=productService.getById(productId);
Product=productService.getById(productId);
JsonNode节点=objectMapper.convertValue(teacher,JsonNode.class);
(ObjectNode)node.put(“customVariable”、“somethingsomething”);
返回node.toString();
}

1-您可以将
自定义变量
字段添加到
产品
实体中

公共类产品{
私有字符串productId;
私有字符串产品名称;
私人双产品价格;
私有字符串图像链接;
私有字符串自定义变量;
公共产品(){
}
//getter/setter。。
}
2-如果要在不修改
产品
实体的情况下添加额外字段:

@GetMapping(“/getProduct”)
公共字符串getProduct(@RequestParam String productId){
Product=productService.getById(productId);
Product=productService.getById(productId);
JsonNode节点=objectMapper.convertValue(teacher,JsonNode.class);
(ObjectNode)node.put(“customVariable”、“somethingsomething”);
返回node.toString();
}

为什么不在
产品
类中添加
自定义变量
此属性?为什么不在
产品
类中添加
自定义变量
此属性?