【问题标题】:How to have lombok to create constructor for non null fields since @RequiredArgsConstructor seems not to work?由于@RequiredArgsConstructor 似乎不起作用,如何让lombok为非空字段创建构造函数?
【发布时间】:2018-04-11 13:47:39
【问题描述】:

我正在玩 Lombok 并且已经浏览了许多链接,但没有一个对我有用。

Person.java

@Setter @Getter
@ToString
@AllArgsConstructor
//@NoArgsConstructor
@RequiredArgsConstructor
@Entity
public class Person {
    @Id
    @GeneratedValue
    private Long id;

    @NotNull
    @Size(min = 1, max = 20)
    private String firstName;

    @NotNull
    @Size(min = 1, max = 50)
    private String lastName;
}

PersonController.java

@RestController
@RequestMapping("/people")
public class PersonController {
    @Autowired
    private PersonRepository personRepository;

    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public void createPerson(@RequestBody Person person) {
        personRepository.save(new Person(person.getFirstName(), person.getLastName()));  //line-34
    }
}

但它不允许我创建两个参数的构造函数

Multiple markers at this line
    - The constructor Person(String, String) is undefined
    - The method save(S) in the type CrudRepository<Person,Long> is not applicable for the arguments 
     (Person)

第 34 行断线...

EDIT-1:

 @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void updatePerson(@PathVariable("id") Long id, @RequestBody Person person) {
        Person existingPerson = personRepository.findOne(id);
        existingPerson.setFirstName(person.getFirstName());
        existingPerson.setLastName(person.getLastName());
        personRepository.save(existingPerson);
    }

这是错误

The method setFirstName(String) is undefined for the type Person

我所做的更改

@Setter @Getter
@ToString
@AllArgsConstructor
//@NoArgsConstructor
@RequiredArgsConstructor()
@Entity
public class Person {
    @Id
    @GeneratedValue
    private Long id;

    @NotNull
    @Size(min = 1, max = 20)
    private final String firstName;

    @NotNull
    @Size(min = 1, max = 50)
    private final String lastName;
} 

-====================

Edit-2

这是最终结果:

@Setter @Getter
@ToString
@AllArgsConstructor
@RequiredArgsConstructor
@Entity
public class Person {
    @Id
    @GeneratedValue
    private Long id;

    @NotNull
    @Size(min = 1, max = 20)
    private String firstName;

    @NotNull
    @Size(min = 1, max = 50)
    private String lastName;

    public Person(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

【问题讨论】:

标签: java spring microservices lombok


【解决方案1】:

您在这些字段上没有 Lomboks @NonNull 注释。我刚才注意到了。

您在这些字段上只有@javax.validation.constraints.NotNull 注释,而@RequiredArsgConstructor 不适用于该字段。

@NotNull 注释之外添加 @NonNull。可能是您不再需要@NotNull,因此也请尝试将其删除。

@NonNull 
@NotNull
@Size(min = 1, max = 20)
private String firstName;

@NonNull 
@NotNull
@Size(min = 1, max = 50)
private String lastName;

【讨论】:

  • 但它破坏了我上面在编辑中显示的其他代码。请出示代码sn-p,以便我理解。
  • 那么解决方案是什么?看起来 Lombok 方法对于 Jpa 和 mongo 来说是不够的
  • 抱歉造成误解,我再次阅读了 lombok 文档并看到了实际问题。
  • 嘿,我不明白你最后的评论。我需要在该模型类中进行哪些更改?请指导
猜你喜欢
  • 2020-07-26
  • 2020-12-02
  • 1970-01-01
  • 2017-06-26
  • 2018-05-15
  • 1970-01-01
  • 1970-01-01
  • 2020-01-29
  • 1970-01-01
相关资源
最近更新 更多