java – 在HATEOAS _embedded PagedResource中命名Spring页面键
作者:互联网
我们目前正在最新的项目中实现API库.
我们正在尝试将Spring HATEOAS与HAL一起用作生成json hal响应的适当库.
使用Spring引导1.2.5.RELEASE提供的Spring HATEOAS版本.
目前,我们正在将要返回的实际JPA实体映射为Resources.
即我们的实体看起来像这样:
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Entity
@Table(name = "users")
public class User {
@Id
private UUID id;
@Column(nullable = false, length = 70)
private String firstName;
@Column(nullable = false, length = 70)
private String lastName;
}
我们的仓库:
public interface UserRepository extends PagingAndSortingRepository<User, UUID>, JpaSpecificationExecutor<User> {
User findByUsername(String username);
}
我们的服务:
@Service
@Transactional
public class UserService implements UserDetailsService {
private UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Transactional
public Page<User> findAll(Pageable pageable) {
return this.userRepository.findAll(pageable);
}
}
RestController:
@RestController
@RequestMapping(value = "/users", produces =MediaType.APPLICATION_JSON_VALUE) // this is done otherwise objectmapper returns xml for some reason
public UserRestController {
public UserService userService;
public EntityLinks entityLinks;
@Autowired
public UserRestController(UserService userService, EntityLinks entityLinks) {
this.userService = userService;
this.entityLinks = entityLinks;
}
@RequestMapping(method = RequestMethod.GET)
public PagedResource<Resource<User>> getUsers(Pageable pageable, PagedResourcesAssembler<User> pagedResourcesAssembler) {
Page<User> userPage = this.userService.findAll(pageable);
PagedResources<Resource<User>> userPagedResources =
pagedResourcesAssembler.toResource(
usersPage,
linkTo(methodOn(UserRestController.class).showUsers(pageable, pagedResourcesAssembler)).withSelfRel());
return userPagedResources;
}
}
假设我将默认页面大小设置为1,页面正在适当地生成.将链接设置为上一页/下一页.正在生成’embeddable’属性,但我们在第一页获得“userList”:[{user}],第二页获取“user $$_ jvst163_9List”:[{user}].
有没有办法实际命名页面列表属性(我一直试图在网上找到一些东西,但无济于事).
例如:
{
"_links": {
"self": {
"href": "http://localhost:8080/users{?page,size,sort}",
"templated": true
},
"next": {
"href": "http://localhost:8080/users?page=1&size=1&sort=firstname,lastname,asc"
}
},
"_embedded": {
"userList": [{
"id": "2027bea9-cfdc-4724-b29c-39b3f64cbfd5",
"firstname": "admin",
"lastname": "asdf",
}]
},
"page": {
"size": 1,
"totalElements": 2,
"totalPages": 2,
"number": 0
}
}
{
"_links": {
"self": {
"href": "http://localhost:8080/users{?page,size,sort}",
"templated": true
},
"prev": {
"href": "http://localhost:8080/users?page=0&size=1&sort=firstname,lastname,asc"
}
},
"_embedded": {
"user_$$_jvst163_9List": [{
"id": "52c0c09e-c386-4aec-9723-f8beaf99adc5",
"username": "admin",,
"firstname": "firstname",
"lastname": "lastname"
}]
},
"page": {
"size": 1,
"totalElements": 2,
"totalPages": 2,
"number": 1
}
}
解决方法:
据我了解作者自己对该问题的评论,找到的解决方案是将@Relation(collectionRelation =“users”)添加到User类.
...
@Table(name = "users")
@Relation(collectionRelation = "users")
public class User {
...
标签:java,spring,spring-boot,spring-hateoas 来源: https://codeday.me/bug/20190824/1708430.html