javaeclipsemavenspring-bootspring-hateoas

Missing linkTo and methodOn declarations Spring HATEOAS STS


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:


Solution

  • 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