Hateoas-找不到适合链接的构造函数(java.lang.string)

发布于 2025-01-26 15:36:24 字数 1031 浏览 5 评论 0原文

对于REST API,在控制器中,我正在应用Hateoas。在方法中添加链接的一部分时,我会收到以下错误: 无法

在pom.xml中解析构造函数'link(string)':

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

代码如下:

 @GetMapping
public @ResponseBody ResponseEntity<List<UserResponseDTO>> get() {

    // Retrieve users
    List<UserResponseDTO> responseDTOS = new ArrayList<>();
    List<User> users = userService.getUsers();

    // Convert to responseDTOS
    for (User user : users) {
        UserResponseDTO userResponseDTO = new UserResponseDTO(user.getId(), user.getFirstName(), user.getLastName());

        Link get = new Link("http://localhost:8081/user/").withRel("GET");

        userResponseDTO.add(get);
        responseDTOS.add(userResponseDTO);
    }

    return new ResponseEntity<>(responseDTOS, HttpStatus.OK);
}

有人知道如何解决这个问题吗?

For a REST API, in the controller I'm applying hateoas. When adding the part of Link in the methods, I get the follow error:
Cannot resolve constructor 'Link(String)'

In the pom.xml:

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

The code is as follows:

 @GetMapping
public @ResponseBody ResponseEntity<List<UserResponseDTO>> get() {

    // Retrieve users
    List<UserResponseDTO> responseDTOS = new ArrayList<>();
    List<User> users = userService.getUsers();

    // Convert to responseDTOS
    for (User user : users) {
        UserResponseDTO userResponseDTO = new UserResponseDTO(user.getId(), user.getFirstName(), user.getLastName());

        Link get = new Link("http://localhost:8081/user/").withRel("GET");

        userResponseDTO.add(get);
        responseDTOS.add(userResponseDTO);
    }

    return new ResponseEntity<>(responseDTOS, HttpStatus.OK);
}

Does anyone know how to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

治碍 2025-02-02 15:36:24

链接(字符串)被弃用,可以在某些新版本中删除。另外,链接(字符串)使用受保护的访问修饰符,这意味着您应仅从同一软件包访问它。

您仍然可以使用的静态方法创建链接,该方法通过公共访问修改器定义了 static方法。

所以应该是

Link get = Link.of("http://localhost:8081/user/").withRel("GET");

Link(String) is deprecated and may be removed in some new version. Also Link(String) uses the protected access modifier meaning you should access it only from the same package.

You can still create the Link using the of static method which by the way is defined with public access modifier.

So it should be

Link get = Link.of("http://localhost:8081/user/").withRel("GET");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文