简体   繁体   中英

Cannot import HATEOAS elements in Spring Boot Gradle project

I can't import any HATEOAS elements, although it seems to be correctly implemented in my build.gradle:

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

And here are my imports:

import org.springframework.hateoas.Resource;
import org.springframework.hateoas.Resources;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;

And here are the errors:

$ ./gradlew build

> Task :compileJava FAILED
path\src\main\java\payroll\EmployeeController.java:5: error: cannot find symbol
import org.springframework.hateoas.Resource;
                                  ^
  symbol:   class Resource
  location: package org.springframework.hateoas
path\src\main\java\payroll\EmployeeController.java:6: error: cannot find symbol
import org.springframework.hateoas.Resources;
                                  ^
  symbol:   class Resources
  location: package org.springframework.hateoas
path\src\main\java\payroll\EmployeeController.java:15: error: package org.springframework.hateoas.mvc does not exist
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;
                                             ^
path\src\main\java\payroll\EmployeeController.java:41: error: cannot find symbol
  Resource<Employee> one(@PathVariable Long id) {
  ^
  symbol:   class Resource
  location: class EmployeeController
4 errors

I don't think it's relevant, but I am using IntelliJ and am trying to work through this tutorial: https://spring.io/guides/tutorials/bookmarks/

I couldn't find any solutions when I googled the issue, and I don't really understand what the problem is, so I don't know what else to try.

Having researched a bit more, I realized that the spring.io tutorial is outdated. After playing around a little, IntelliJ started showing org.springframework.hateoas, but the imports provided by the tutorial still didn't work.

I eventually found the link to the source code, and the code has been updated, while the tutorial has not.

https://github.com/spring-guides/tut-rest/tree/master/rest/src/main/java/payroll

Basically, Resource has been replaced with EntityModel, Resources has been replaced with CollectionModel, and the structure of the imports has changed.

After updating my code to match the source code, I got the expected response when I sent a GET request for an Employee:

{"id":1,"name":"Bilbo Baggins","role":"burglar","_links":{"self":{"href":"http://localhost:8080/employees/1"},"employees":{"href":"http://localhost:8080/employees"}}}

You probably found an outdated tutorial. Now you need to migrate the application code to the newer version of HATEOAS.

  • See Migrating to Spring HATEOAS 1.0 guide
  • You can find the official migration script on GitHub. Simply download that, run it from your project root. By default it will inspect all Java source files and replace the legacy Spring HATEOAS type references with the new ones.

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