Java 8 Optional.ofNullable.map 产生非静态方法引用错误
Java 8 Optional.ofNullable.map producing Non static method reference error
我有一种方法可以验证电子邮件的收件人。
在我的代码中 .map(Recipient::getId)
产生错误:
Non static method cannot be reference from a static context.
private Long verifyRecipient(Long recipientId) throws NotFoundException {
return Optional.ofNullable(recipientRepository.findById(recipientId))
.map(Recipient::getId)
.orElseThrow(()-> new NotFoundException("recipient with ID" + recipientId +
" was not found"));
}
Recipient
class:
@Entity
public class Recipient {
@Id
@GeneratedValue
private Long id;
@NotBlank
private String name;
@NotBlank
@Email
@Column(unique = true)
private String emailAddress;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
}
我在内存数据库中使用了SpringBoot和H2。
所以我也有一个RecipientRepository
接口:
public interface RecipientRepository extends JpaRepository<Recipient, Long> {}
findById()
方法的定义:
Optional<T> findById(ID var1);
方法 findById()
已经 returns 一个 Optional<T>
,所以在这种情况下你不需要用额外的 Optional.ofNullable()
包装结果。
实际上,行:
Optional.ofNullable(recipientRepository.findById(recipientId));
returnsOptional<Optional<Recipient>>
,这是多余的。
相反,您可以只写:
private Long verifyRecipient(Long recipientId) throws NotFoundException {
return recipientRepository.findById(recipientId)
.map(Recipient::getId)
.orElseThrow(() ->
new NotFoundException("Recipient with ID " + recipientId + " was not found"));
}
我有一种方法可以验证电子邮件的收件人。
在我的代码中 .map(Recipient::getId)
产生错误:
Non static method cannot be reference from a static context.
private Long verifyRecipient(Long recipientId) throws NotFoundException {
return Optional.ofNullable(recipientRepository.findById(recipientId))
.map(Recipient::getId)
.orElseThrow(()-> new NotFoundException("recipient with ID" + recipientId +
" was not found"));
}
Recipient
class:
@Entity
public class Recipient {
@Id
@GeneratedValue
private Long id;
@NotBlank
private String name;
@NotBlank
@Email
@Column(unique = true)
private String emailAddress;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
}
我在内存数据库中使用了SpringBoot和H2。
所以我也有一个RecipientRepository
接口:
public interface RecipientRepository extends JpaRepository<Recipient, Long> {}
findById()
方法的定义:
Optional<T> findById(ID var1);
方法 findById()
已经 returns 一个 Optional<T>
,所以在这种情况下你不需要用额外的 Optional.ofNullable()
包装结果。
实际上,行:
Optional.ofNullable(recipientRepository.findById(recipientId));
returnsOptional<Optional<Recipient>>
,这是多余的。
相反,您可以只写:
private Long verifyRecipient(Long recipientId) throws NotFoundException {
return recipientRepository.findById(recipientId)
.map(Recipient::getId)
.orElseThrow(() ->
new NotFoundException("Recipient with ID " + recipientId + " was not found"));
}