spring-bootapirestspring-hateoashateoas

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?


Solution

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