我正在将Spring Boot 2.2.0.M1与HATEOAS和Gradle一起使用。
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;
}
}
我该如何解决?
我的目标是从HATEOAS中找到Resource
,并使用ControllerLinkBuilder
的替代品。
答案 0 :(得分:3)
Resource
替换为EntityModel
,ControllerLinkBuilder
替换为WebMvcLinkBuilder
答案 1 :(得分:0)
最根本的变化是Spring HATEOAS不会创建资源。这就是Spring MVC / Spring WebFlux的功能。我们创建超媒体的供应商中立表示。因此,我们重命名了这些核心类型:
LINK- https://spring.io/blog/2019/03/05/spring-hateoas-1-0-m1-released#overhaul
答案 2 :(得分:0)
包装结构的最大变化是通过引入超媒体类型注册API来实现的,以支持Spring HATEOAS中的其他媒体类型。
https://docs.spring.io/spring-hateoas/docs/current/reference/html/
答案 3 :(得分:0)
正如其他编码人员所提到的那样,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>
答案 4 :(得分:0)
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
是您要公开的方法
答案 5 :(得分:0)
链接到其他回复...以下代码对我来说效果很好...
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>