小编Ism*_*yen的帖子

从java8迁移到java9时,对方法的引用是不明确的

我正在将项目从JAVA 8迁移到JAVA 9,我在使代码工作时遇到了一些麻烦.所有工作在JAVA 8但在9我有以下错误:

Error java: reference to ok is ambiguous
      both method <T>ok(java.util.function.Supplier<T>)  and method ok(web.Procedure) match
Run Code Online (Sandbox Code Playgroud)

这是我调用方法时的代码:

public ResponseEntity<List<MailTemplateDto>> mailTemplateFindAll() {
    return ok(() -> mailTemplateService.findAll());
}
Run Code Online (Sandbox Code Playgroud)

这是实施:

 public <T> ResponseEntity<T> ok(Supplier<T> action) {
     return this.body(HttpStatus.OK, action);
 }

 public <T> ResponseEntity<T> ok(T body) {
     return this.ok(() -> {
         return body;
     });
 }

 public ResponseEntity<Void> ok(Procedure action) {
     action.invoke();
     return this.status(HttpStatus.OK);
 }

 public ResponseEntity<Void> ok() {
     return this.status(HttpStatus.OK);
 }
Run Code Online (Sandbox Code Playgroud)

Procedure接口的代码:

@FunctionalInterface
public interface Procedure {
    void invoke();
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?


可重现的代码 …

java compiler-errors java-8 functional-interface java-9

11
推荐指数
1
解决办法
406
查看次数