使用Lombok创建指定字段构造器遇报错:如何生成仅两字段构造器?
Got it, let's break down how to generate a constructor for exactly two fields in your Category class while using Lombok, and fix that visibility error you're seeing.
First, Understand the Root of the Error
That "not visible" message usually boils down to one of two issues:
- The two-parameter constructor you're trying to use doesn't exist at all (Lombok didn't generate it as you expected)
- The constructor does exist, but it's marked as
privateor package-private, so yourApp.javacan't access it
Solution 1: Manually Write the Constructor (Simplest & Most Predictable)
Lombok plays perfectly nice with manually written code—you don't have to rely solely on annotations. If you only need a constructor for two specific fields, just write it out directly alongside your Lombok annotations:
import lombok.Data; import java.util.List; @Data // Generates getters, setters, toString, equals/hashCode, and a no-arg constructor public class Category { // Fields you want in the constructor private String name; private String description; // Other fields private Long id; private List<Product> products; // Your custom two-field constructor public Category(String name, String description) { this.name = name; this.description = description; } }
This works flawlessly—Lombok won't overwrite or conflict with your manual constructor. You can now call new Category("Books", "Fiction and non-fiction") in App.java without any visibility issues.
Solution 2: Use Lombok's @RequiredArgsConstructor (Auto-Generate the Constructor)
If you prefer to let Lombok handle constructor generation, use @RequiredArgsConstructor along with either final or @NonNull on the fields you want included. Lombok will automatically create a public constructor for all fields marked this way:
import lombok.Data; import lombok.NonNull; import lombok.RequiredArgsConstructor; import java.util.List; @Data @RequiredArgsConstructor // Generates constructor for @NonNull/final fields public class Category { // Mark fields to include with @NonNull @NonNull private String name; @NonNull private String description; // Other fields (not included in constructor) private Long id; private List<Product> products; }
Or if you prefer using final instead of @NonNull:
import lombok.Data; import lombok.RequiredArgsConstructor; import java.util.List; @Data @RequiredArgsConstructor public class Category { private final String name; private final String description; private Long id; private List<Product> products; }
Both approaches will generate a public Category(String, String) constructor that your App.java can access freely.
How to Avoid the Visibility Trap
If you still get the "not visible" error after trying these, double-check:
- You didn't set the
accessparameter on@RequiredArgsConstructorto something restrictive likeAccessLevel.PRIVATE(the default isAccessLevel.PUBLIC, which is what you want) - You're not mixing conflicting annotations (e.g.,
@NoArgsConstructoralone won't generate your two-arg constructor—you need to add the custom constructor or@RequiredArgsConstructor)
内容的提问来源于stack exchange,提问作者Jeff Cook

