缺少 linkTo 和 methodOn 声明 Spring HATEOAS STS

Missing linkTo and methodOn declarations Spring HATEOAS STS

我正在关注 Spring RESTfull API 教程。本教程要求在某些时候使用 Spring HATEOAS。但是,我的 IDE、STS、 找不到方法的引用,linkTo 和 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>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-hateoas</artifactId>
    </dependency>

我尝试过的东西:

这里linkTomethodOnorg.springframework.hateoas.mvc.ControllerLinkBuilderclass的两个静态方法。您只需要将以下两个静态导入语句添加到 class:

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

或者只导入 ControllerLinkBuilder 并将它们用作:

ControllerLinkBuilder.linkTo
ControllerLinkBuilder.methodOn

我正在学习相同的教程,但在使用方法“linkTo”和“methodOn[=32=”时遇到了同样的问题]".

似乎导入应该来自:

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

但是,它似乎已被弃用,现在建议使用 WebMvcLinkBuilder:

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

我在这个 class 中找到了线索: https://github.com/spring-projects/spring-hateoas-examples/blob/master/simplified/src/main/java/org/springframework/hateoas/examples/EmployeeController.java

此外,在教程页面的底部有一个 link 到 GitHub 完整项目的 repo: https://github.com/spring-guides/tut-rest

我还发现 运行在遵循教程时使用“LoadDatabase.java”存在问题。为了解决这个问题,我必须让它实现 CommandLineRunner 并将原始代码放入它的 运行 方法中:

@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;
}

我就是这样用的,效果很好

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"));
}

由于事实:WebMvcLinkBuilder 如此处所述,已弃用: WebMvcLinkBuilder java docs

应改用 WebMvcLinkBuilder。

WebMvcLinkBuilder java docs

考虑使用进口:

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;