9

I've been trying to create a simple hello world application with Java and SpringBoot in IntelliJ IDEA, but I get this error and I don't know how to solve it.

I get the error at the return. Java doesn't seem to know how to resolve the of method that's in the public List<String> getHelloWorld method.

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");
    }
}

rgettman
  • 176,041
  • 30
  • 275
  • 357
M.Gumede
  • 137
  • 1
  • 2
  • 7
  • 1
    What's your Java version? [`List.of`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html#of(E,E)) was new in Java 9. – rgettman Apr 15 '19 at 17:43
  • 2
    The `List.of(..)` is available in JDK9+ as far as I know. So my assumption is you are using JDK8... – khmarbaise Apr 15 '19 at 17:43
  • 1
    You can use `Arrays.asList` in earlier Java versions (`Collections.unmodifableList(Arrays.asList(...))`, if you're being picky). – Andy Turner Apr 15 '19 at 17:55

2 Answers2

19

The overloaded List.of methods were introduced in Java 9.

Since:
9

You must be compiling with an older version. Update to Java 9 or later. Here's how.

Savior
  • 3,225
  • 4
  • 24
  • 48
2

You can use Arrays.asList.

List.of will support java 9 and above

Rijo
  • 2,963
  • 5
  • 35
  • 62