简体   繁体   中英

Hateoas - No suitable constructor found for Link(java.lang.String)

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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