如何使用findBy spring存储库在java中检索Map中的数据

时间:2017-05-19 10:34:03

标签: java spring spring-mvc couchbase spring-data-couchbase

我需要帮助为以下场景创建findBy spring查询: 我有一个JSON文档,其结构如下:

"data":{
"key1":"value1",
"key2":"value2"
}

在模型中,我将此“数据”视为地图,

Map<String, Object> data;
public Map<String, Object> getData() {
        return data;
    }
    public void setData(Map<String, Object> data) {
        this.data = data;
    }

现在,我想使用spring存储库从数据中获取value2。我正在使用couchbase for DB。

任何帮助都会非常明显。

提前致谢。

1 个答案:

答案 0 :(得分:0)

 @Entity
 public class Obj{
  private Integer id;
  private String name;
   //getter and setter
 }

 @Controller
 public class ControllerClass{
    @Autowired
    private ObjService objService;

    @GetMapping("/getObjectById/{id}") 
    @ResponseBody
    public Map<String, Obj> getMapDetails(@PathVariable Integer id) {
        Map<String, Obj> map = new HashMap<>();     
        map.put("data",objService.findById(id));    
        /*here you can able to N of times 
          Ex: map.put("data",service2.findById(id));
          and etc...
        */
        return map;
   }
 }

 @Service
 public Class ObjService{
    public Obj findById(Integer id){
       //logic
    }
 }

 your Response will be like below:
 {"data":{
        "id":1,
        "name":"value2"
   }
 }