导入org.springframework.hateoas.mvc无法解析

问题描述

嗨,我是Spring Boot的新手,正在尝试使用REST服务创建一个简单的Spring Boot项目。 我正在尝试导入org.springframework.hateoas.mvc.ControllerLinkBuilder。*;并使用linkTo方法,但这给了我一个无法解决的错误。 这是我的pom.xml文件-

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-hateoas -->
        <dependency>
           <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-hateoas</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

这也是我的控制器文件-

package com.example.demo;

import javax.persistence.EntityNotFoundException;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.Resources;

@RestController
public class EmployeeController {
    private final EmployeeRepository repository;
    EmployeeController(EmployeeRepository repository){
        this.repository=repository;
    }
    @GetMapping("/employees")
    java.util.List<Employee> all(){
        return repository.findAll();
    }
    
    @PostMapping("/employees")
    Employee newEmployee(Employee employee) {
        return repository.save(employee);
    }
    @GetMapping("/employees/{id}")
    Employee one(@PathVariable Long id) {

          Employee employee = repository.findById(id)
            .orElseThrow(() -> new EmployeeNotFoundException(id));

          return new Resource<>(employee,linkTo(methodOn(EmployeeController.class).one(id)).withSelfRel(),linkTo(methodOn(EmployeeController.class).all()).withRel("employees"));
    }
    
    @PutMapping("/employees/{id}")
      Employee replaceEmployee(@RequestBody Employee newEmployee,@PathVariable Long id) {

        return repository.findById(id)
          .map(employee -> {
            employee.setName(newEmployee.getName());
            employee.setRole(newEmployee.getRole());
            return repository.save(employee);
          })
          .orElseGet(() -> {
            newEmployee.setId(id);
            return repository.save(newEmployee);
          });
      } 
    
    @DeleteMapping("/employees/{id}")
    void deleteEmployee(@PathVariable Long id) {
        repository.deleteById(id);
    }
    
}

我该怎么办?

解决方法

某些类的名称已更改,某些包已移至其他位置。如果您想了解更多信息,请参阅https://docs.spring.io/spring-hateoas/docs/current/reference/html/#migrate-to-1.0.changes.representation-models

,

尝试:

导入静态org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; 或者只是。*

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...