gpt4 book ai didi

java - 在 HATEOAS _embedded PagedResource 中命名 Spring 页面键

转载 作者:搜寻专家 更新时间:2023-11-01 01:50:57 25 4
gpt4 key购买 nike

我们目前正在我们最新的项目中实现一个 API 库。我们正在尝试使用带有 HAL 的 Spring HATEOAS 作为适当的库来生成 json hal 响应。

使用 Spring boot 1.2.5.RELEASE + 提供的 Spring HATEOAS 版本。目前我们正在映射要作为资源返回的实际 JPA 实体。

即我们的实体看起来像这样:

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,页面将被适本地生成。设置指向上一页/下一页的链接。正在生成“可嵌入”属性,但随后我们在第一页上得到“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 - 在 HATEOAS _embedded PagedResource 中命名 Spring 页面键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32400801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com