简体   繁体   English

Java Lombok:省略@AllArgsConstructor 中的一个字段?

[英]Java Lombok: Omitting one field in @AllArgsConstructor?

If I specify @AllArgsConstructor using Lombok , it will generate a constructor for setting all the declared (not final, not static) fields.如果我使用Lombok指定@AllArgsConstructor ,它将生成一个构造函数来设置所有声明的(非最终的,非静态的)字段。 Is it possible to omit some field and this leave generated constructor for all other fields?是否可以省略某个字段,而这会为所有其他字段生成构造函数?

No that is not possible.不,那是不可能的。 There is a feature request to create a @SomeArgsConstructor where you can specify a list of involved fields.有一个创建@SomeArgsConstructor的功能请求,您可以在其中指定相关字段的列表。

Full disclosure: I am one of the core Project Lombok developers.完全披露:我是龙目岛项目的核心开发人员之一。

Alternatively, you could use @RequiredArgsConstructor .或者,您可以使用@RequiredArgsConstructor This adds a constructor for all fields that are either @NonNull or final .这为@NonNullfinal所有字段添加了一个构造函数。

See the documentation查看文档

在某些情况下,解决它的一个好方法是使用@Builder

Just in case it helps, initialized final fields are excluded.以防万一,初始化的final字段被排除在外。

@AllArgsConstructor
class SomeClass {
    final String s;
    final int i;
    final List<String> list = new ArrayList<>(); // excluded in constructor
}

var x = new SomeClass("hello", 1);

It makes sense especially for collections, or other mutable classes.这对于集合或其他可变类尤其有意义。

This solution can be used together with the other solution here , about using @RequiredArgsConstructor :此解决方案可以与此处其他解决方案一起使用,关于使用@RequiredArgsConstructor

@RequiredArgsConstructor
class SomeClass2 {
    final String s;
    int i; // excluded because it's not final
    final List<String> list = new ArrayList<>(); // excluded because it's initialized
}

var x = new SomeClass2("hello");

You can initialise those particular fields with some default values or null and then create the constructor omitting those fields. 您可以使用某些默认值或null初始化这些特定字段,然后创建省略这些字段的构造函数。 But remember, now you can't pass the omitted fields in the constructor anymore. 但是请记住,现在您不能再在构造函数中传递省略的字段。

@AllArgsConstructor
Public class MyClass{
        @JsonProperty
        String omitThisField = null;
        @JsonProperty
        String reqField1;
        @JsonProperty 
        String reqField2;
}

MyClass myClass = new MyClass("req field1", "req field2");

But this won't work now: 但这现在不起作用:

//MyClass myClass = new MyClass("omitted field", "req field1", "req field2");

This can be done using two annotations from lombok @RequiredArgsConstructor and @NonNull .这可以使用来自lombok @RequiredArgsConstructor@NonNull 的两个注释来完成。

Please find the example as follows请查找示例如下

package com.ss.model;

import lombok.*;

@Getter
@Setter
@RequiredArgsConstructor
@ToString
public class Employee {

    private int id;
    @NonNull
    private String firstName;
    @NonNull
    private String lastName;
    @NonNull
    private int age;
    @NonNull
    private String address;
}

And then you can create a constructor as below然后你可以创建一个构造函数如下

Employee employee = new Employee("FirstName", "LastName", 27, "Address");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM