0

我的“表示”类中有两个构造函数,它们创建两个不同的 JSON 对象,这些对象被我的项目中的不同服务使用。可以说他们是:

@XmlRootElement
public class Rules {

    @XmlElement
    private String name;

    @XmlElement
    private String direction;

    @XmlElement
    private String protocol;

    public Rules(NetworkSecurityRule rule) {
        this.name = rule.name();
        this.direction = rule.direction().toString();
        this.protocol=rule.protocol().toString();
    }

    public Rules(String name, String direction,String protocol) {
        this.name = name;
        this.direction = direction;
        
    }

    return "Rules{" +
                    "name='" + name + '\'' +
                    ", direction='" + direction + '\'' +
                    ", protocol='" + protocol + '\'' +
                '}';

我的问题是,如何表示上面的 twoString() 方法来创建基于第二个构造函数的 JSON?我在代码的另一部分调用第二个构造函数来创建 JSON ,但我得到以下 JSON

我当前返回的 JSON 是这样的:我不希望返回空值。

[ {
  "name" : "AllowHttpsInBound",
  "direction" : "Inbound",
  "protocol" : null
}, {
  "name" : "AllowAzureInBound",
  "direction" : "Inbound",
  "protocol" : null
}]

我想要这个:

[ {
      "name" : "AllowHttpsInBound",
      "direction" : "Inbound"
    }, {
      "name" : "AllowAzureInBound",
      "direction" : "Inbound"
 }]
4

0 回答 0