12

I am following Spring RESTfull API tutorial. The tutorial asks to use Spring HATEOAS at some point. However, my IDE, STS, cannot find the references of the methods, linkTo and methodOn.

@GetMapping("/employees/{id}")
Resource<Employee> one(@PathVariable Long id) {
 Employee emp = repository.findById(id)
  .orElseThrow(() -> new EmployeeNotFoundException(id));

 return new Resource<>(emp,
        linkTo(methodOn(EmployeeController.class).one(id)).withSelfRel(),
        linkTo(methodOn(EmployeeController.class).all()).withRel("employees")
 );
}

Spring HATEOAS dependency is also here:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-hateoas</artifactId>
    </dependency>

The things I have tried:

  • Updating maven project

5 Answers 5

23

I am following the same tutorial and came across the same problem with the methods "linkTo" and "methodOn".

It seems like the import should be from:

import static org.springframework.hateoas.server.mvc.ControllerLinkBuilder.*;

However, it seems it is already deprecated and there is now WebMvcLinkBuilder suggested to be used:

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;

I found clues in this class: https://github.com/spring-projects/spring-hateoas-examples/blob/master/simplified/src/main/java/org/springframework/hateoas/examples/EmployeeController.java

More over, at the bottom of the tutorial page there is a link to the GitHub repo of complete project: https://github.com/spring-guides/tut-rest

I also found problems running the "LoadDatabase.java" when following the tutorial. To fix this, I had to make it implement the CommandLineRunner and put the original code inside it's run method:

@Component
public class LoadDatabase implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(LoadDatabase.class);

    @Override
    public void run(String... args) throws Exception {

        employeeRepository.save(new Employee("Bilbo", "Baggins", "burglar"));
        employeeRepository.save(new Employee("Frodo", "Baggins", "thief"));

        employeeRepository.findAll().forEach(employee -> log.info("Preloaded " + employee));

        orderRepository.save(new Order("MacBook Pro", Status.COMPLETED));
        orderRepository.save(new Order("iPhone", Status.IN_PROGRESS));

        orderRepository.findAll().forEach(order -> {
            log.info("Preloaded " + order);
        });
    }

    @Autowired
    EmployeeRepository employeeRepository;
    @Autowired
    OrderRepository orderRepository;
}
2
  • 1
    Best answer to the question.
    – theRiley
    Sep 21, 2020 at 3:09
  • 1
    Yes, run into all these problems with the same Spring tutorial. Very crappy and inaccurately written
    – pixel
    Jul 2, 2022 at 18:31
8

Here linkTo and methodOn are two static methods of org.springframework.hateoas.mvc.ControllerLinkBuilder class. You just need to add below two static import statements to your class:

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

OR just import ControllerLinkBuilder and use them as:

ControllerLinkBuilder.linkTo
ControllerLinkBuilder.methodOn
4
  • 2
    The tutorial does not mention neither 'import static' nor 'ControllerLinkBuilder' itself. Is this a tutorial documentation issue or we are missing something else? Jul 23, 2019 at 14:33
  • 2
    I think the tutorial should at least mention the class name containing these methods - otherwise how a new user is gonna find the class name. Jul 23, 2019 at 14:44
  • This actually isn't working for me. import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.* resolves only linkTo and methodOn but not one() and all()
    – pixel
    Jul 2, 2022 at 18:34
  • @pixel Make sure you've created those methods yourself. Both should exist if you've followed the tutorial correctly. all() is under @GetMapping("/employees") and one() is under @GetMapping("/employees/{id}") Aug 26, 2022 at 14:20
4

I used like that and it worked

import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder;


@GetMapping("/employees/{id}")
EntityModel<Employee> one(@PathVariable Long id) {
    Employee employee = repository.findById(id)
            .orElseThrow(() -> new EmployeeNotFoundException(id));
    return EntityModel.of(employee, //
            WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(EmployeeController.class).one(id)).withSelfRel(),
            WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(EmployeeController.class).all()).withRel("employees"));
}
1

due to fact that: ControllerLinkBuilder is depreacted as mentioned here: ControllerLinkBuilder java docs

WebMvcLinkBuilder should be used instead.

WebMvcLinkBuilder java docs

Consider using imports:

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
1
  • none of the solutions here worked for me. while this resolves only linkTo and methodOn but not one() and all()
    – pixel
    Jul 2, 2022 at 18:37
0

Looks like you need two imports:

  • import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*; >>for the methodOn

  • import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder; >> for instatiating WebMvcLinkBuilder

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.