Java 缺少指向Spring HATEOAS STS的链接和方法声明
我正在学习SpringRESTFullAPI教程。本教程要求在某个时候使用Spring HATEOAS。但是,我的IDE、STS、找不到方法、链接到和methodOn的引用Java 缺少指向Spring HATEOAS STS的链接和方法声明,java,eclipse,maven,spring-boot,spring-hateoas,Java,Eclipse,Maven,Spring Boot,Spring Hateoas,我正在学习SpringRESTFullAPI教程。本教程要求在某个时候使用Spring HATEOAS。但是,我的IDE、STS、找不到方法、链接到和methodOn的引用 @GetMapping("/employees/{id}") Resource<Employee> one(@PathVariable Long id) { Employee emp = repository.findById(id) .orElseThrow(() -> new EmployeeNo
@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")
);
}
@GetMapping(“/employees/{id}”)
资源一(@PathVariable Long id){
Employee emp=repository.findById(id)
.orelsetrow(()->new EmployeeNotFoundException(id));
返回新资源(emp,
链接到(methodOn(EmployeeController.class).one(id)).withSelfRel(),
链接到(methodOn(EmployeeController.class).all()).withRel(“雇员”)
);
}
Spring HATEOAS依赖项也在这里:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
org.springframework.boot
弹簧靴启动器hateoas
我尝试过的事情:
- 更新maven项目
linkTo
和methodOn
是org.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
我正在学习相同的教程,在使用方法“linkTo”和“methodOn”时遇到了相同的问题 似乎进口应来自:
import static org.springframework.hateoas.server.mvc.ControllerLinkBuilder.*;
但是,它似乎已经被弃用,现在建议使用WebMvcLinkBuilder:
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
我在这节课上找到了线索:
此外,在教程页面的底部有一个链接,指向完整项目的GitHubrepo:
在学习本教程时,我还发现运行“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;
}
我就这样用过,而且很管用
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"));
}
import org.springframework.hateoas.EntityModel;
导入org.springframework.hateoas.server.mvc.WebMvcLinkBuilder;
@GetMapping(“/employees/{id}”)
EntityModel one(@PathVariable Long id){
Employee=repository.findById(id)
.orelsetrow(()->new EmployeeNotFoundException(id));
返回(员工)的实体模型//
WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(EmployeeController.class).one(id)).with selfrel(),
WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(EmployeeController.class).all()).withRel(“雇员”);
}
本教程既没有提到“导入静态”也没有提到“ControllerLinkBuilder”。这是一个教程文档问题还是我们缺少了其他东西?我认为教程至少应该提到包含这些方法的类名-否则新用户将如何找到类名。这个问题的最佳答案。