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

3
  • 1
    What's your Java version? List.of was new in Java 9. Commented Apr 15, 2019 at 17:43
  • 2
    The List.of(..) is available in JDK9+ as far as I know. So my assumption is you are using JDK8... Commented Apr 15, 2019 at 17:43
  • 1
    You can use Arrays.asList in earlier Java versions (Collections.unmodifableList(Arrays.asList(...)), if you're being picky). Commented Apr 15, 2019 at 17:55

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

3

You can use Arrays.asList.

List.of will support java 9 and above

Comments

0

To make this code compatible with Java 8, you'll need to adjust the usage of List.of(), which was introduced in Java 9, and use a method that works in Java 8, such as Arrays.asList().


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

@RestController
public class HelloWorldController {

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

Explanation:

List.of() is not available in Java 8, so I've replaced it with Arrays.asList(), which creates a fixed-size list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.