实体中的布尔字段在 JSON 中更改名称。

huangapple go评论81阅读模式
英文:

Boolean field in entity changes name in JSON

问题

有一些奇怪的事情发生在我的应用程序中,我想知道原因。

我有一个资源,在我的Spring服务器中与后端服务一起构建,当JSON到达前端时,其中一个属性名称不同。我已经通过断点跟踪了整个资源的构建过程,在返回查询之前,属性名称从未从`isHiddenOnQuote`更改过 - 正如您可能猜到的,它在对象模型中被定义为布尔值。我的数据库将该值存储为1或0。

当我的前端收到JSON时,属性名称更改为`hiddenOnQuote` - “is”神奇地消失了。同样奇怪的是,我在JSON中有其他布尔字段,它们没有更改;它们保留了它们的“is”。

以下是模型的一部分代码。请注意,这些属性在超类BaseEntity中都不存在。

package com.company.app.model.sales;

import com.company.app.model.BaseEntity;
import lombok.Data;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.List;

@Data
@Entity
@SQLDelete(sql =
"UPDATE product_option_category " +
"SET is_deleted = true " +
"WHERE id = ?")
@Where(clause = "is_deleted = false")

public class ProductOptionCategory extends BaseEntity {
private String categoryName;

private int optionLimit;

private int mnSegment;

private boolean isBitwise;

private boolean areOptionsRepeatable = false;

private boolean isHiddenOnQuote = false;

public boolean getIsBitwise() {
    return isBitwise;
}

}


这是Lombok的特性吗?
英文:

Something curious is happening in my application and I would like to know the reason.

I have a resource that is constructed with a backend service in my Spring server, and when the JSON arrives at the front end one of the property names is different. I have followed with breakpoints the entire construction of the resource and at no point before returning the query does the property name ever change from isHiddenOnQuote - as you probably suspect, it's defined as a boolean in the object model. My database stores the value as a 1 or 0.

When the JSON is received by my front end, the property name changes to hiddenOnQuote - the "is" magically drops off. Strange as well, is the fact that I have other boolean fields in the JSON that don't change; they retain their "is"ness.

Here is a snippet from the model. Note that none of these properties exist in the superclass, BaseEntity.

package com.company.app.model.sales;

import com.company.app.model.BaseEntity;
import lombok.Data;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.List;

@Data
@Entity
@SQLDelete(sql =
        "UPDATE product_option_category " +
                "SET is_deleted = true " +
                "WHERE id = ?")
@Where(clause = "is_deleted = false")

public class ProductOptionCategory extends BaseEntity {
    private String categoryName;

    private int optionLimit;

    private int mnSegment;

    private boolean isBitwise;

    private boolean areOptionsRepeatable = false;

    private boolean isHiddenOnQuote = false;

    public boolean getIsBitwise() {
        return isBitwise;
    }
}

Is this a Lombok thing?

答案1

得分: 1

似乎你可能遇到了一个问题,如果 isBitwise 能正常工作但 isHiddenOnQuote 不能的话。

另外,注意到你没有使用 Lombok 中的 @Getter 等注解。也许你可以尝试使用以下注解来强制使用正确的名称:

@get:JsonProperty("isHiddenOnQuote")

@param:JsonProperty("isHiddenOnQuote")

我从 Stackoverflow 上的这个回答中得到了这个信息:
https://stackoverflow.com/a/55100741/4402505

编辑:修正了属性名称。

英文:

Seems like you may have hit upon a bug if isBitwise works well but not isHiddenOnQuote.

Also, noticed that you are not using the @Getter etc annotations from Lombok. Perhaps you can try using the following annotations to force the correct name:

@get:JsonProperty("isHiddenOnQuote")

@param:JsonProperty("isHiddenOnQuote")

I got this from this answer on Stackoverflow:
https://stackoverflow.com/a/55100741/4402505

EDIT: Fixed the property name.

答案2

得分: 1

是的,Lombok根据Java命名约定(https://www.comp.nus.edu.sg/~cs2103/AY1617S1/contents/coding-standards-java.html)为原始布尔变量生成方法,前缀为“is”。
我们可以使用@Getter注解对该字段进行注解:

@Getter
private boolean running;

然后,Lombok将使用其注解处理器在类中生成一个isRunning()方法。

有时候会出现冲突。
假设我们需要在同一个类中拥有以下行:

@Getter
public boolean running = true;

@Getter
public boolean isRunning = false;

我们应该避免使用这种令人困惑的命名约定,有很多原因。其中之一是它会为Lombok创建冲突。

根据Lombok的约定,这两个字段将具有相同的访问器方法名:isRunning。但是在同一个类中使用相同名称的两个方法将创建编译错误。

Lombok通过只创建一个访问器方法来解决此问题,在这种情况下,它将基于字段声明顺序仅指向running

英文:

Yes, Lombok generate for primitive boolean variable method with prefix "is" according to Java naming convention (https://www.comp.nus.edu.sg/~cs2103/AY1617S1/contents/coding-standards-java.html).
We can annotate that field with @Getter:

@Getter
private boolean running;

And Lombok will use its annotation processor to generate an isRunning() method in the class.

Sometimes, there can be conflicts.
Let's say that we need to have the following lines in the same class:

@Getter
public boolean running = true;

@Getter
public boolean isRunning = false;

There are many reasons we should avoid a confusing naming convention like this. One of them is that it creates a conflict for Lombok.

Using Lombok's convention, these two fields would have the same accessor method name: isRunning. But having two methods with the same name in the same class will create a compiler error.

Lombok solves this by creating only one accessor method and, in this case, pointing it at running, based on field declaration order.

huangapple
  • 本文由 发表于 2020年9月10日 00:29:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63815827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定