猿问

缺少 linkTo 和 methodOn 声明 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>

我尝试过的事情:


更新maven项目


RISEBY
浏览 167回答 5
5回答

ABOUTYOU

我遵循相同的教程并遇到了方法“ linkTo ”和“ methodOn ”的相同问题。似乎导入应该来自:import static org.springframework.hateoas.server.mvc.ControllerLinkBuilder.*;但是,它似乎已被弃用,现在建议使用 WebMvcLinkBuilder :import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;我在这个类中找到了线索: https://github.com/spring-projects/spring-hateoas-examples/blob/master/simplified/src/main/java/org/springframework/hateoas/examples/EmployeeController.java此外,在教程页面的底部有一个指向完整项目的GitHub存储库的链接: https://github.com/spring-guides/tut-rest在按照教程进行操作时,我还发现运行“LoadDatabase.java”时出现问题。为了解决这个问题,我必须让它实现 CommandLineRunner并将原始代码放入它的运行方法中:@Componentpublic class LoadDatabase implements CommandLineRunner {&nbsp; &nbsp; private static final Logger log = LoggerFactory.getLogger(LoadDatabase.class);&nbsp; &nbsp; @Override&nbsp; &nbsp; public void run(String... args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; employeeRepository.save(new Employee("Bilbo", "Baggins", "burglar"));&nbsp; &nbsp; &nbsp; &nbsp; employeeRepository.save(new Employee("Frodo", "Baggins", "thief"));&nbsp; &nbsp; &nbsp; &nbsp; employeeRepository.findAll().forEach(employee -> log.info("Preloaded " + employee));&nbsp; &nbsp; &nbsp; &nbsp; orderRepository.save(new Order("MacBook Pro", Status.COMPLETED));&nbsp; &nbsp; &nbsp; &nbsp; orderRepository.save(new Order("iPhone", Status.IN_PROGRESS));&nbsp; &nbsp; &nbsp; &nbsp; orderRepository.findAll().forEach(order -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.info("Preloaded " + order);&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; @Autowired&nbsp; &nbsp; EmployeeRepository employeeRepository;&nbsp; &nbsp; @Autowired&nbsp; &nbsp; OrderRepository orderRepository;}

蛊毒传说

这里linkTo和methodOn是类的两个静态方法org.springframework.hateoas.mvc.ControllerLinkBuilder。您只需要在您的类中添加以下两个静态导入语句:import&nbsp;static&nbsp;org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; import&nbsp;static&nbsp;org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;或者只是导入ControllerLinkBuilder并将它们用作:ControllerLinkBuilder.linkTo ControllerLinkBuilder.methodOn

白板的微信

我那样用过并且有效import org.springframework.hateoas.EntityModel;import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder;@GetMapping("/employees/{id}")EntityModel<Employee> one(@PathVariable Long id) {&nbsp; &nbsp; Employee employee = repository.findById(id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .orElseThrow(() -> new EmployeeNotFoundException(id));&nbsp; &nbsp; return EntityModel.of(employee, //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(EmployeeController.class).one(id)).withSelfRel(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(EmployeeController.class).all()).withRel("employees"));}

海绵宝宝撒

由于以下事实:ControllerLinkBuilder 已弃用,如下所述: ControllerLinkBuilder java docs应该改用 WebMvcLinkBuilder。WebMvcLinkBuilder java文档考虑使用进口:import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

回首忆惘然

看起来你需要两个导入:import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;>>对于方法论import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder;>> 用于实例化 WebMvcLinkBuilder
随时随地看视频慕课网APP

相关分类

Java
我要回答