I am currently learning Spring Boot through the official tutorial available at https://spring.io/guides/tutorials/rest. While working on the tutorial, my code runs smoothly, but when using VSCode, I encounter several yellow squiggles indicating null type safety issues
Null type safety: The expression of type 'Employee' needs unchecked conversion to conform to '@NonNull Employee'Java(16778128)Null type safety: The expression of type 'EntityModel<Employee>' needs unchecked conversion to conform to '@NonNull Object'Java(16778128)
I understand that these warnings can be addressed by modifying the code, but I am curious if VSCode has the capability to automatically detect that the employee object is not null in this context.
The relevant code snippet is as follows:
@GetMapping("/employees/{id}")
EntityModel<Employee> one(@PathVariable @NonNull Long id) {
Employee employee = repository.findById(id)
.orElseThrow(() -> new EmployeeNotFoundException(id));
return EntityModel.of(employee,
linkTo(methodOn(EmployeeController.class).one(id)).withSelfRel(),
linkTo(methodOn(EmployeeController.class).all()).withRel("employees"));
}
While the code executes without issues, the yellow squiggles make me wonder if there's a way for VSCode to infer that employee is guaranteed to be non-null at this point.
Is there a specific setting, extension, or analysis tool in VSCode that can help address or suppress these warnings, considering the specific logic in this code?
Thank you for your guidance!
I've modified my code:
@SuppressWarnings("null")
@GetMapping("/employees/{id}")
EntityModel<Employee> one(@PathVariable @NonNull Long id) {
Employee employee = repository.findById(id)
.orElseThrow(() -> new EmployeeNotFoundException(id));
return EntityModel.of(employee,
linkTo(methodOn(EmployeeController.class).one(id)).withSelfRel(),
linkTo(methodOn(EmployeeController.class).all()).withRel("employees"));
}