带有 MongoDB 的 Java Spring Boot——并非所有文档字段都映射到 POJO

问题描述 投票:0回答:0

我有一个 MongoDB 数据库,其中填充了代表游戏资产的 JSON 文件。

以下是示例中引用的两个 JSON 文档:

{
  "name": "Kaelyssa, The Night's Whisper",
  "edition": 3,
  "revision": "2021 v1",
  "keywords": [
    "Retribution",
    "Mage Hunter",
    "Warcaster"
  ],
  "warjack points": 29,
  "field allowance": "C",
  "models": [
    {
         // SUB-DOCUMENT CONTAINING LOTS OF IRRELEVANT INFORMATION OMITTED FOR READABILITY
    }
  ]
}

{
  "name": "Mortitheurge Willbreaker",
  "edition": 3,
  "revision": "2021 v1",
  "keywords": [
    "Skorne",
    "Solo"
  ],
  "point cost": 2,
  "field allowance": 2,
  "models": [
    {
         // SUB-DOCUMENT CONTAINING LOTS OF IRRELEVANT INFORMATION OMITTED FOR READABILITY
    }
  ]
}

这里是与问题相关的模型类(为了便于阅读,省略了像 getters、setters 和 imports 这样的样板代码):

@Document("cards")
public class Card {

    private ObjectId id;
    
    @BsonProperty(value = "name")
    private String name;
    
    @BsonProperty(value = "edition")
    private Integer edition;
    
    @BsonProperty(value = "revision")
    private String revision;
    
    @BsonProperty(value = "keywords")
    private ArrayList<String> keywords;
    
    @BsonProperty(value = "unit size")
    private String unitSize;
    
    @BsonProperty(value = "warjack points")
    private Integer warjackPoints;
    
    @BsonProperty(value = "warbeast points")
    private Integer warbeastPoints;
    
    @BsonProperty(value = "point cost")
    private Integer pointCost;
    
    @BsonProperty(value = "field allowance")
    private String fieldAllowance;
    
    @BsonProperty(value = "models")
    private ArrayList<Model> models;
}

这是我最小的 Repository 类:

public interface CardRepository extends MongoRepository<Card, String>{

    @Query("{name:'?0'}")
    Card findCardByName(String name);
    
    public long count();
}

这是我的申请文件:

@SpringBootApplication
public class GameApplication implements CommandLineRunner {
    
    @Autowired
    CardRepository cardRepository;
    
    public static void main(String[] args) {
        SpringApplication.run(CollegeApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {

        findCard();
    }   

    public void findCard() {
        long count = cardRepository.count();
        System.out.println("Number of cards found: " + count);

        String cardName = "Kaelyssa, The Night's Whisper";
        Card card = cardRepository.findCardByName(cardName);
        System.out.println(card);

        cardName = "Mortitheurge Willbreaker";
        card = cardRepository.findCardByName(cardName);
        System.out.println(card);
    }
}

最后,这是我运行应用程序时的输出:

Number of cards found: 1209
Card [id=640f7ae7d0371a1460ba8e7f, name=Kaelyssa, The Night's Whisper, edition=3, revision=2021 v1, keywords=[Retribution, Mage Hunter, Warcaster], unitSize=null, warjackPoints=null, warbeastPoints=null, pointCost=null, fieldAllowance=null, models=[com.kriegzeug.college.com.model.Model@661e279d]]
Card [id=640f7ae9b608c31e106c5097, name=Mortitheurge Willbreaker, edition=3, revision=2021 v1, keywords=[Skorne, Solo], unitSize=null, warjackPoints=null, warbeastPoints=null, pointCost=null, fieldAllowance=null, models=[com.kriegzeug.college.com.model.Model@621624b1]]

如您所见,我的应用程序大部分连接正确。

我得到了

cardRepository.count()
方法的正确输出,并且
cardRepository.findCardByName()
方法确实检索了正确的文档。

我不明白的是为什么字段

name
edition
revision
keywords
models
都被正确映射,但是字段
unitSize
warjackPoints
 warbeastPoints
pointCost
fieldAllowance
都返回
null
,即使该字段存在值也是如此。请注意,一个文档有一个
point cost
字段而另一个没有,一个文档有一个
warjack points
字段而另一个没有并且两个文档都有一个
field allowance
字段。

可能有一个线索,即非填充字段在 JSON 文档中都有键,这些键是由空格分隔的两个词(例如“字段津贴”)。但是

@BsonProperty(value = "field allowance")
注释不应该说明这一点吗?

谁能给我提供线索,说明为什么我的 MongoDB 文档没有正确映射到我的 Java POJO?谢谢!

java mongodb spring-boot spring-data-mongodb
© www.soinside.com 2019 - 2024. All rights reserved.