简体   繁体   English

SimpleIdentifiableResourceAssembler 无法解析为类型

[英]SimpleIdentifiableResourceAssembler cannot be resolved to a type

I am following this tutorial: Spring HATEOAS - Basic Example我正在学习本教程: Spring HATEOAS - 基本示例

And I have a spring boot project with the following dependencies:我有一个 spring 引导项目,具有以下依赖项:

<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-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.8.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.8.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-data-rest</artifactId>
        <version>2.8.0</version>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.9.0</version>
    </dependency>
</dependencies>

The tutorial shows that Spring HATEOAS provides SimpleIdentifiableResourceAssembler as the simplest mechanism to perform conversions.本教程显示 Spring HATEOAS 提供了 SimpleIdentifiableResourceAssembler 作为执行转换的最简单机制。

The problem is that I cannot resolve the class "SimpleIdentifiableResourceAssembler" from the org.springframework.hateoas packages.. It throws an SimpleIdentifiableResourceAssembler cannot be resolved to a type问题是我无法从 org.springframework.hateoas 包中解析 class“SimpleIdentifiableResourceAssembler”。它抛出一个SimpleIdentifiableResourceAssembler cannot be resolved to a type

You don't have the HATEOAS dependency in your project. 您的项目中没有HATEOAS依赖项。

Add

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-hateoas</artifactId>
    </dependency>

HATEOAS dependency is added in: HATEOAS依赖项添加到:

在此处输入图片说明

But this does not solve the problem 但这不能解决问题

Even though it's sort of late but for future (and confused) developers: The answer is simply that the Class 即使有点晚,但对于未来(和困惑的)开发人员来说:答案很简单,就是Class

SimpleIdentifiableResourceAssembler

has not been added to the Repo so far. 到目前为止尚未添加到Repo中。 You have to implement the functionality on your own (see Github issue for this question ) 您必须自己实现功能( 有关此问题,请参阅Github问题

In my project, I use RepresentationModelAssembler it is part of在我的项目中,我使用RepresentationModelAssembler它是

org.springframework.hateoas.server.RepresentationModelAssembler;

example:例子:

@Component
public class AccountModelAssembler implements RepresentationModelAssembler<AccountResponse, EntityModel<AccountResponse>> {

  @Override
  public @NonNull CollectionModel<EntityModel<AccountResponse>> toCollectionModel(@NonNull Iterable<? extends AccountResponse> entities) {
    return RepresentationModelAssembler.super.toCollectionModel(entities);
  }

  
  @Override
  public @NonNull EntityModel<AccountResponse> toModel(@NonNull AccountResponse entity) {
    // Do your mapping and link generation here.
  }
}

And AccountResponse is like this:AccountResponse是这样的:

@JsonInclude(JsonInclude.Include.NON_NULL)
public record AccountResponse(
    Integer id,
    String code,
    String name,
    String description,
    AccountTypeEnum type
) {
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM