我正在玩龙目岛并且已经通过了许多链接,但它们都没有为我工作。
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号线上破坏......
修改-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;
}
- ===================
修改-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;
}
}
答案 0 :(得分:2)
您在这些字段上没有Lomboks @NonNull
注释。我刚才注意到了。
您在这些字段上只有@javax.validation.constraints.NotNull
注释ṣ@RequiredArsgConstructor
不适用于此。
除@NonNull
注释外,还添加@NotNull
。可能您不再需要@NotNull
,所以也尝试将其删除。
@NonNull
@NotNull
@Size(min = 1, max = 20)
private String firstName;
@NonNull
@NotNull
@Size(min = 1, max = 50)
private String lastName;