简体   繁体   English

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

[英]How to retrieve data within a Map in java using findBy spring repository

I need help in creating a findBy spring query for the following scenario: I have a JSON document with the following structure: 我需要在以下情况下创建findBy弹簧查询时需要帮助:我有一个具有以下结构的JSON文档:

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

In model, I have this"data" as a Map like, 在模型中,我将此“数据”作为地图,例如

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

Now, I want to get the value2 from data using spring repository. 现在,我想使用Spring存储库从数据中获取value2。 I'm using couchbase for DB. 我正在使用Couchbase数据库。

Any help would be really appreciable. 任何帮助都是非常可观的。

Thanks in advance. 提前致谢。

 @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"
   }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM