有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java通过REST、Spring访问MongoDB中的嵌套数据

与我之前的question类似,我试图使用Spring REST访问MongoDB中的数据

我收集了一些简单的键值对,可以很好地访问它们

{
    "_id" : ObjectId("5874ab4a19b38fb91fbb234f"),
    "roID" : "7ed3f9a6-bb9b-4d16-8d1a-001b7ec40b51",
    "Name" : "[REDACTED]"
}

问题是,这些对象在另一个集合中使用,该集合显示了它们之间的属性关系,如下所示:

{
    "_id" : ObjectId("5874ab4f19b38fb91fbb6180"),
    "[OBJECT CATEGORY A]" : {
        "_id" : ObjectId("5874ab4a19b38fb91fbb257b"),
        "roID" : "72f8a8b5-71a7-40ac-b1ac-1ffc98a507ba",
        "Name" : "[REDACTED]"
    },
    "[OBJECT CATEGORY B]" : {
        "_id" : ObjectId("5874ab4b19b38fb91fbb32a3"),
        "roID" : "919446ab-1898-419f-a704-e8c34985f945",
        "Name" : "[REDACTED]"
    },
    "[RELATIONSHIP INFORMATION]" : [ 
        {
            "[PROPERTY A]" : [ 
                {
                    "[VALUE A]" : 5.0
                }, 
                {
                    "[VALUE B]" : 0.0
                }
            ]
        }, 

房产在8到20之间

Java中第一个(普通)对象的定义如下:

@Document(collection="OBJ")
public class Obj {

    public Obj(){};

    @Id
    public String id;

    @Field("roID")
    public String roID;

    @Field("Name")
    public String name;

}

存储库类:

@RepositoryRestResource(collectionResourceRel = "OBJ", path = "OBJ")
public interface ObjRepo extends MongoRepository<Obj, String> {

    List<Obj> findByName(@Param("name") String name); 
}

问题是:如何访问嵌套对象?我已经尝试使用LinkedHashMap代替复杂集合的字符串,当我尝试访问它们时,curl只返回“null”。我试着定义一个类

public class BITS {
    @Id
    private String _id;
    @Field("roID")
    private String roID;
    @Field("Name")
    private String name;

    public BITS(){}

    public BITS(String _id,String roID, String name){
        this._id = _id;
        this.roID = roID;
        this.name = name;
    }

}

无法访问这些对象,但未成功


共 (1) 个答案

  1. # 1 楼答案

    结果证明类方法是正确的,只是执行得不好。 出于测试目的,我创建了一个简单的JSON集合:

    @Document(collection="JSON")
    public class JSON {
    
        @Id
        public String id;
    
        @Field("squares")
        public Square square;
    
        @Field("dots")
        public Dot dot;
    
        public JSON(){};
        public JSON(String id, Square square,Dot dot){
            this.id = id;
            this.square = square;
            this.dot = dot;
        };
    
    
    }
    

    广场。爪哇

    public class Square {
    
        private String id;
        private int x;
        private int y;
    
        public Square(){};
    
        public Square(String id,int x, int y){
            this.id = id;
            this.x = x;
            this.y = y;
        };
    
        public Map<String, Integer> getSquare()
        {
        Map<String, Integer> res = new HashMap<>();
        res.put("x", x);
        res.put("y", y);
        return res;
    }
    
    }
    

    (点是一样的,只是为了测试) 因此,它只是精确地重塑所需的响应,尽管数据库中已经有这种格式的响应。 如果有人能告诉我在哪里可以消除响应tho中的混乱,那就太好了。现在看起来是这样的:

    "_embedded" : {
        "JSON" : [ {
          "square" : null,
          "dot" : {
            "dot" : {
              "x" : 4,
              "y" : 3
            }
          },
          "_links" : {
            "self" : {
              "href" : "http://localhost:8080/JSON/58ac466160fb39e5e8dc8b70"
            },
            "jSON" : {
              "href" : "http://localhost:8080/JSON/58ac466160fb39e5e8dc8b70"
            }
          }
        }, {
          "square" : {
            "square" : {
              "x" : 12,
              "y" : 2
            }
          },
          "dot" : null,
          "_links" : {
            "self" : {
              "href" : "http://localhost:8080/JSON/58ac468060fb39e5e8dc8b7e"
            },
            "jSON" : {
              "href" : "http://localhost:8080/JSON/58ac468060fb39e5e8dc8b7e"
            }
          }
        } ]
      },
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/JSON"
        },
        "profile" : {
          "href" : "http://localhost:8080/profile/JSON"
        }
      },
      "page" : {
        "size" : 20,
        "totalElements" : 2,
        "totalPages" : 1,
        "number" : 0
      }
    }