首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我在我的控制器中接收邮递员请求的空值?

为什么我在我的控制器中接收邮递员请求的空值?
EN

Stack Overflow用户
提问于 2019-07-15 07:33:52
回答 1查看 2.7K关注 0票数 0

我编写了一个rest控制器,它接收请求并将数据保存在db中。这是我使用邮递员发送的post请求

代码语言:javascript
复制
{
    "product_id" : "testproduct",
    "default_concurrent":10,
    "default_connections":1000,
    "msan":10
}

我的实体类:

代码语言:javascript
复制
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;

import org.springframework.data.jpa.domain.support.AuditingEntityListener;

@Entity
@Table(name="products")
@EntityListeners(AuditingEntityListener.class)

public class Product {

    @Id
    @Column(name="product_id", nullable = false)
    private String product_id;

    @Column(name="default_concurrent", nullable = true)
    private int default_concurrent;

    @Column(name="default_connections", nullable = true)
    private int default_connections;

    @Column(name="msan", nullable = true)
    private int msan;



    public String getProduct() {
        return product_id;
    }

    public void setProduct(String product) {
        this.product_id = product;
    }

    public int getDefault_concurrent() {
        return default_concurrent;
    }

    public void setDefault_concurrent(int default_concurrent) {
        this.default_concurrent = default_concurrent;
    }

    public int getDefault_connections() {
        return default_connections;
    }

    public void setDefault_connections(int default_connections) {
        this.default_connections = default_connections;
    }

    public int getMsan() {
        return msan;
    }

    public void setMsan(int msan) {
        this.msan = msan;
    }


}

我的控制器:

代码语言:javascript
复制
    @PostMapping("/add")
    public Product addProduct(@Valid @RequestBody Product product) 
    {
        return productDao.saveProduct(product);
    }

错误:

代码语言:javascript
复制
ids for this class must be manually assigned before calling save()
nested exception is org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.test.model.Product",

我已经在SO上看过很多与此错误相关的文章,其中大多数建议使用

@GeneratedValue( @GeneratedValue(strategy=GenerationType.IDENTITY) )或strategy=GenerationType.AUTO

但我不能使用它,因为我的主键将是字符串,我不能改变它的长。

在调试过程中,我可以看到,在请求体中,我接收product_id为null。其余属性具有正确的值。

但是,如果我在DB中再创建一个作为'Id‘的列,并相应地更新实体类,将'Id’作为主键并尝试执行,那么我将在请求体中获得正确的值,并且它也会保存在DB中。

需要帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-15 08:46:12

因为我们将下划线字符视为保留字符,所以我们强烈建议遵循标准的Java命名约定(也就是说,不要在属性名称中使用下划线,而是使用camel大小写)。

文档

下划线_数据查询派生中的保留字符,可能允许手动属性路径描述。

坚持使用骆驼箱作为成员变量名的Java命名约定,一切都将如预期的那样工作。

在使用JPA功能时,您也将面临一个问题。所以我建议改变命名惯例。变化

代码语言:javascript
复制
    private String product_id;
    private int default_concurrent;
    private int default_connections;
    private int msan;

到,

代码语言:javascript
复制
private String productId;
private int defaultConcurrent;
private int defaultConnections;
private int msan;

UPDATE1

为什么product_id仅为null

要知道我在IDE中粘贴了相同的代码,并发现实体产品缺少用于字段product_id的getter-setter。添加后,相同的代码将值保存到数据库中。但是,我仍然建议每个人避免字段的_ in名称,以避免进一步的问题,并按照文档的方式遵循命名约定。在使用JPA时,例如findBy(实体中有_ in名称的内容),您可能面临类似于的问题

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57035143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档