无法解析方法“List.of(java.lang.String, java.lang.String)”

M.G*_*ede 2 java intellij-idea

我一直在尝试在 IntelliJ IDEA 中使用 Java 和 SpringBoot 创建一个简单的 hello world 应用程序,但出现此错误,我不知道如何解决。

我在返回时收到错误。Java 似乎不知道如何解析of方法中的public List<String> getHelloWorld方法。

package com.myname.SpringApp;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;


@RestController
public class HelloWorldController {
    @RequestMapping("api/hello-world")
    @GetMapping
    public List<String> getHelloWorld(){
        return List.of("Hello", "World");
    }
}

Run Code Online (Sandbox Code Playgroud)

Sav*_*ior 7

Java 9 中引入了重载List.of方法

自:
9

您必须使用旧版本进行编译。更新到 Java 9 或更高版本。就是这样。