【问题标题】:Spring Data MongoDB: Parameter marked with @Indexed not wirtten in DBSpring Data MongoDB:标有@Indexed的参数未写入数据库
【发布时间】:2021-12-27 13:41:56
【问题描述】:

我的第一个 Mongo DB 春季后端遇到了一些问题。我创建了一个具有一些属性的组件类。其中之一是零件号,该零件号应是唯一的。所以我设置了@Indexed (unique = true) 注释。

应用程序运行良好。但是,如果我使用邮递员测试端点,所有属性都将写入 Mongo 文档,但部件号除外。 PN 总是空的。我不明白为什么。如果有人可以帮助我解决他的问题,我会很高兴。

这是组件类

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

import lombok.Data;

@Data
@Document
public class Component {
    @Id
    private String id;
    private String cType;
    @Indexed(unique = true)
    private String pN;
    private String name;
    private double diameter;
    private double bendradius;
    private double maxLenght;
    private String modelPath;
    private String description;
    private String normOrStandard;
}

这是一个对路由 http://localhost:8080/components 的 post 请求示例

    {

        "PN":"13245"
    }

结果:

{
    "id": "61c9c23960435c3c0c4d5844",
    "name": null,
    "diameter": 0.0,
    "bendradius": 0.0,
    "maxLenght": 0.0,
    "modelPath": null,
    "description": null,
    "normOrStandard": null,
    "ctype": null,
    "pn": null,
    "_links": {
        "self": {
            "href": "http://localhost:8080/components/61c9c23960435c3c0c4d5844"
        },
        "components": {
            "href": "http://localhost:8080/components"
        }
    }
}

如果所有字段都已填写,也会发生这种情况。

【问题讨论】:

    标签: java spring mongodb


    【解决方案1】:

    尝试使用与 Component 类中的字段匹配的区分大小写的 HTTP 参数运行 Postman 请求:

    {
       "pN":"13245"
    }
    

    代替:

    {
       "PN":"13245"
    }
    

    【讨论】:

    • 哇,很棒的小费。有效。它区分大小写。解决方案不是 "pN":"13245" 而是小写字母 (pn :)
    猜你喜欢
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 2017-11-30
    • 1970-01-01
    相关资源
    最近更新 更多