Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resource and ControllerLinkBuilder not found and deprecated

I am using Spring Boot 2.2.0.M1 with HATEOAS and Gradle.

implementation 'org.springframework.boot:spring-boot-starter-hateoas'

Right now, Resource is not found by the IDE (IntelliJ IDEA 2018.3) and ControllerLinkBuilder is marked as deprecated.

package com.example.restfulwebservicegradle.user;

import static org.springframework.hateoas.server.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

import com.example.restfulwebservicegradle.User;
import com.example.restfulwebservicegradle.UserDaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.server.mvc.ControllerLinkBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.validation.Valid;
import java.net.URI;
import java.util.List;

@RestController
public class UserResource {

    @Autowired
    private UserDaoService service;

    @GetMapping("users/{id}")
    public Resource<User> retrieveUser(@PathVariable int id) {
        User user = service.findOne(id);

        if (user == null)
            throw new UserNotFoundException("id-" + id);


        // Resource not found
        Resource<User> resource = new Resource<User>(user);

        // Deprecated
        ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveAllUsers());

        resource.add(linkTo.withRel("all-users"));

        return resource;
    }
}

The imports available according to the IDE are: enter image description here

How can I solve this?

My goal is to found Resource from HATEOAS and to use the substitute of ControllerLinkBuilder.

like image 745
RRGT19 Avatar asked Apr 20 '19 04:04

RRGT19


1 Answers

The most fundamental change is the fact that Spring HATEOAS doesn’t create resources. That’s what Spring MVC/Spring WebFlux does. We create vendor neutral representations of hypermedia. So we renamed those core types:

LINK- https://spring.io/blog/2019/03/05/spring-hateoas-1-0-m1-released#overhaul

  1. ResourceSupport is now RepresentationModel
  2. Resource is now EntityModel
  3. Resources is now CollectionModel
  4. PagedResources is now PagedModel
like image 192
Antim Avatar answered Oct 11 '22 09:10

Antim