缺少LinkTo和Method On声明Spring HATEOAS STS

j0pj023g  于 2022-09-21  发布在  Spring
关注(0)|答案(4)|浏览(232)

我正在学习Spring RESTull API教程。本教程要求在某个时候使用Spring HATEOAS。但是,我的IDE、STS、找不到方法LinkTo和Method On的引用

@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项目
mctunoxg

mctunoxg1#

我正在学习相同的教程,并在方法“Linkto”和“Method On”中遇到了相同的问题。

似乎应该从以下位置导入:

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

此外,在教程页面的底部有一个链接,指向完整项目的GitHubrepo:https://github.com/spring-guides/tut-rest

在学习本教程时,我还发现运行“LoadDatabase.java”时出现了问题。要解决这个问题,我必须让它实现CommandLineRunner,并将原始代码放入它的run方法中:

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

t9aqgxwy2#

这里的linkTomethodOnorg.springframework.hateoas.mvc.ControllerLinkBuilder类的两个静态方法。您只需在您的类中添加以下两条静态导入语句:

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

或者只导入ControllerLinkBuilder并将其用作:

ControllerLinkBuilder.linkTo
ControllerLinkBuilder.methodOn
niknxzdl

niknxzdl3#

我就是这么用的,而且很管用

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

mlnl4t2r4#

由于以下事实:ControllerLinkBuilder的解压方式如下:ControllerLinkBuilder java docs

应改用WebMvcLinkBuilder。

WebMvcLinkBuilder java docs

考虑使用导入:

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

相关问题