Missing linkTo and methodOn declarations Spring HATEOAS STS
Asked Answered
I

5

13

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
Impersonal answered 23/7, 2019 at 13:58 Comment(0)
J
9

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
Jerilynjeritah answered 23/7, 2019 at 14:11 Comment(4)
The tutorial does not mention neither 'import static' nor 'ControllerLinkBuilder' itself. Is this a tutorial documentation issue or we are missing something else?Impersonal
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.Jerilynjeritah
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()Endomorphic
@Endomorphic 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}")Mcelhaney
S
24

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;
}
Squalene answered 5/7, 2020 at 14:3 Comment(2)
Best answer to the question.Cleora
Yes, run into all these problems with the same Spring tutorial. Very crappy and inaccurately writtenEndomorphic
J
9

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
Jerilynjeritah answered 23/7, 2019 at 14:11 Comment(4)
The tutorial does not mention neither 'import static' nor 'ControllerLinkBuilder' itself. Is this a tutorial documentation issue or we are missing something else?Impersonal
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.Jerilynjeritah
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()Endomorphic
@Endomorphic 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}")Mcelhaney
I
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"));
}
Infuscate answered 20/4, 2021 at 8:36 Comment(0)
P
2

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;
Pliny answered 23/11, 2021 at 12:37 Comment(1)
none of the solutions here worked for me. while this resolves only linkTo and methodOn but not one() and all()Endomorphic
B
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

Bathulda answered 3/10, 2022 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.