Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • Updating maven project
like image 478
Eray Tuncer Avatar asked Jul 23 '19 13:07

Eray Tuncer


People also ask

How do you implement HATEOAS in REST spring boot?

Let's implement the HATEOAS in the project. Step1: Open the pom. xml and add the spring-boot-starter-hateoas dependency. Step 2: Open UserResource.

Why is there HATEOAS in spring?

Spring HATEOAS provides some APIs to ease creating REST representations that follow the HATEOAS principle when working with Spring and especially Spring MVC. The core problem it tries to address is link creation and representation assembly.

How do you implement HATEOAS?

To implement HATEOAS, we would need to include related resources in the response. Instead of Student we use a return type of EntityModel<Student> . EntityModel is a simple class wrapping a domain object and allows adding links to it. We create a new resource.


1 Answers

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;
}
like image 123
Mariusz Lewandowski Avatar answered Sep 20 '22 23:09

Mariusz Lewandowski