未找到资源和 ControllerLinkBuilder 并已弃用
Resource and ControllerLinkBuilder not found and deprecated
我正在使用 Spring 带有 HATEOAS 和 Gradle 的 Boot 2.2.0.M1。
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
现在,IDE(IntelliJ IDEA 2018.3)找不到 Resource
,ControllerLinkBuilder
被标记为 已弃用.
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;
}
}
根据 IDE 可用的进口有:
我该如何解决这个问题?
我的目标是从 HATEOAS 找到 Resource
并使用 ControllerLinkBuilder
的替代品。
最根本的变化是 Spring HATEOAS 不创建资源。这就是 Spring MVC/Spring WebFlux 所做的。我们创建超媒体的供应商中立表示。所以我们重命名了那些核心类型:
LINK- https://spring.io/blog/2019/03/05/spring-hateoas-1-0-m1-released#overhaul
- ResourceSupport 现在是 RepresentationModel
- 资源现在是 EntityModel
- Resources 现在是 CollectionModel
- PagedResources 现在是 PagedModel
包结构的最大变化是引入了超媒体类型注册 API 以支持 Spring HATEOAS 中的其他媒体类型。
https://docs.spring.io/spring-hateoas/docs/current/reference/html/
Resource
被EntityModel
取代,ControllerLinkBuilder
被WebMvcLinkBuilder
取代
正如其他编码员所提到的,HATEOAS 发生了许多变化(弃用),因此请使用以下代码:
@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 has been replaced by EntityModel
EntityModel<User> resource = EntityModel.of(user);
//ControllerLinkBuilder has been replace by WebMvcLinkBuilder
Link link= WebMvcLinkBuilder.linkTo(methodOn(this.getClass()).retrieveAllUsers()).withRel("all-users");
resource.add(link);
return resource;
}
}
这是 spring-hateoas 依赖项,您应该将其添加到 pom.xml
:
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
UserResource controller = methodOn(UserResource.class);
EntityModel<User> resource = EntityModel.of(user);
Link link= WebMvcLinkBuilder.linkTo(controller
.retriveAllUser()).withRel("all-users");
resource.add(link);
return resource;
这里
UserResource
是控制器
retriveAllUser
是您要公开的方法
链接到其他回复...以下代码对我来说工作正常...
UserResource.java
package com.---.---.restfulwebservices.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder;
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;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
@RestController
public class UserResource {
@Autowired
private UserDaoService service;
@GetMapping(path = "/users")
public List<User> retrieveAllUsers() {
return service.findAll();
}
@GetMapping(path = "/users/{id}")
public EntityModel<User> retrieveUser(@PathVariable int id) {
User user = service.findOne(id);
if (null == user)
throw new UserNotFoundException("id-" + id);
EntityModel<User> entityModel = EntityModel.of(user);
Link link= WebMvcLinkBuilder.linkTo(
methodOn(this.getClass()).retrieveAllUsers()).withRel("all-users");
entityModel.add(link);
return entityModel;
}
@PostMapping(path = "/users")
public ResponseEntity<Object> createUser(@Valid @RequestBody User user) {
User newUser = service.saveUser(user);
URI path = ServletUriComponentsBuilder.fromCurrentRequest()
.path("/{id}").buildAndExpand(newUser.getId()).toUri();
return ResponseEntity.created(path).build();
}
@DeleteMapping(path = "/users/{id}")
public User deleteUser(@PathVariable int id) {
User deletedUser = service.deleteUserByID(id);
if (null == deletedUser)
throw new UserNotFoundException("id-" + id);
return deletedUser;
}
}
pom.xml 依赖关系:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
我正在使用 Spring 带有 HATEOAS 和 Gradle 的 Boot 2.2.0.M1。
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
现在,IDE(IntelliJ IDEA 2018.3)找不到 Resource
,ControllerLinkBuilder
被标记为 已弃用.
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;
}
}
根据 IDE 可用的进口有:
我该如何解决这个问题?
我的目标是从 HATEOAS 找到 Resource
并使用 ControllerLinkBuilder
的替代品。
最根本的变化是 Spring HATEOAS 不创建资源。这就是 Spring MVC/Spring WebFlux 所做的。我们创建超媒体的供应商中立表示。所以我们重命名了那些核心类型:
LINK- https://spring.io/blog/2019/03/05/spring-hateoas-1-0-m1-released#overhaul
- ResourceSupport 现在是 RepresentationModel
- 资源现在是 EntityModel
- Resources 现在是 CollectionModel
- PagedResources 现在是 PagedModel
包结构的最大变化是引入了超媒体类型注册 API 以支持 Spring HATEOAS 中的其他媒体类型。
https://docs.spring.io/spring-hateoas/docs/current/reference/html/
Resource
被EntityModel
取代,ControllerLinkBuilder
被WebMvcLinkBuilder
正如其他编码员所提到的,HATEOAS 发生了许多变化(弃用),因此请使用以下代码:
@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 has been replaced by EntityModel
EntityModel<User> resource = EntityModel.of(user);
//ControllerLinkBuilder has been replace by WebMvcLinkBuilder
Link link= WebMvcLinkBuilder.linkTo(methodOn(this.getClass()).retrieveAllUsers()).withRel("all-users");
resource.add(link);
return resource;
}
}
这是 spring-hateoas 依赖项,您应该将其添加到 pom.xml
:
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
UserResource controller = methodOn(UserResource.class);
EntityModel<User> resource = EntityModel.of(user);
Link link= WebMvcLinkBuilder.linkTo(controller
.retriveAllUser()).withRel("all-users");
resource.add(link);
return resource;
这里
UserResource
是控制器
retriveAllUser
是您要公开的方法
链接到其他回复...以下代码对我来说工作正常...
UserResource.java
package com.---.---.restfulwebservices.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder;
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;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
@RestController
public class UserResource {
@Autowired
private UserDaoService service;
@GetMapping(path = "/users")
public List<User> retrieveAllUsers() {
return service.findAll();
}
@GetMapping(path = "/users/{id}")
public EntityModel<User> retrieveUser(@PathVariable int id) {
User user = service.findOne(id);
if (null == user)
throw new UserNotFoundException("id-" + id);
EntityModel<User> entityModel = EntityModel.of(user);
Link link= WebMvcLinkBuilder.linkTo(
methodOn(this.getClass()).retrieveAllUsers()).withRel("all-users");
entityModel.add(link);
return entityModel;
}
@PostMapping(path = "/users")
public ResponseEntity<Object> createUser(@Valid @RequestBody User user) {
User newUser = service.saveUser(user);
URI path = ServletUriComponentsBuilder.fromCurrentRequest()
.path("/{id}").buildAndExpand(newUser.getId()).toUri();
return ResponseEntity.created(path).build();
}
@DeleteMapping(path = "/users/{id}")
public User deleteUser(@PathVariable int id) {
User deletedUser = service.deleteUserByID(id);
if (null == deletedUser)
throw new UserNotFoundException("id-" + id);
return deletedUser;
}
}
pom.xml 依赖关系:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>