spring-guides / tut-rest

Building REST services with Spring :: Learn how to easily build RESTful services with Spring

Home Page:https://spring.io/guides/tutorials/rest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error: The method methodOn(Class) is undefined for the type EmployeeController.

AAdewunmi opened this issue · comments

Hi,

I would like to suggest an udate to section: "What makes something RESTful?"

Users of the guide are not intuitively aware that the import:
"import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;"
will fix the error:
The method methodOn(Class) is undefined for the type EmployeeController.

The WebMvcLinkBuilder import could be placed directly within the code snippet and a link to
Spring HATEOAS - Reference Documentation placed below the code.

import static 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, //
      linkTo(methodOn(EmployeeController.class).one(id)).withSelfRel(),
      linkTo(methodOn(EmployeeController.class).all()).withRel("employees"));
}

Spring HATEOAS - Reference Documentation

I don't mind being assigned this issue.

😎

I've submiited a Pull request for this issue. Guess it's time to close it 😎

Thought I'd keep this issue open incase someone encounters this error ...

Yes, I encountered this error. Looks like they fixed the code but the guide still seems to be incomplete.

Hi @AAdewunmi. Thank you for the issue. This particular bit of code was causing some confusion and spawned a few different issues in the tracker. I pushed a PR #127 that should address this issue, specifically with this line.

Please try again and let us know if things can still be improved. For now I will close the issue as I believe #127 should have cleared things up. Thanks again for raising this.