Spring Hateoas ControllerLinkBuilder 添加空字段 [英] Spring Hateoas ControllerLinkBuilder adds null fields
问题描述
我正在学习有关 Spring REST 的教程,并尝试将 HATEOAS 链接添加到我的控制器结果中.
我有一个简单的 User 类和一个 CRUD 控制器.
class 用户 {私有整数 ID;私人字符串名称;私人 LocalDate 生日;//和 getter/setter}
服务:
@Component类用户服务{私有静态列表<用户>用户 = 新的 ArrayList();列出<用户>查找全部(){返回 Collections.unmodifiableList(users);}公共可选<用户>findById(int id) {返回 users.stream().filter(u -> u.getId() == id).findFirst();}//当然还有添加和删除方法,但这里不重要}
除了在我的控制器中,一切正常,我想将所有用户列表中的链接添加到单个用户:
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;导入静态 org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;@RestController公共类用户控制器{@自动连线私人用户服务用户服务;@GetMapping("/用户")公共列表<资源<用户>>getAllUsers() {列表<资源<用户>>userResources = userService.findAll().stream().map(u -> new Resource<>(u, linkToSingleUser(u))).collect(Collectors.toList());返回用户资源;}链接 linkToSingleUser(用户用户){返回链接(methodOn(UserController.class).getById(user.getId())).withSelfRel();}
这样对于结果列表中的每个用户,都会添加一个指向用户本身的链接.
链接本身创建得很好,但生成的 JSON 中有多余的条目:
<预><代码>[{身份证":1,"name": "亚当","生日": "2018-04-02",链接":[{"rel": "自我","href": "http://localhost:8080/users/1",hreflang":空,媒体":空,标题":空,类型":空,弃用":空}]}]具有空值的字段(hreflang
、media
等)来自哪里,为什么要添加它们?有没有办法摆脱它们?
在建立指向所有用户列表的链接时它们不会出现:
@GetMapping("/users/{id}")公共资源<用户>getById(@PathVariable("id") int id) {最终用户用户 = userService.findById(id).orElseThrow(() -> new UserNotFoundException(id));链接 linkToAll = linkTo(methodOn(UserController.class).getAllUsers()).withRel("所有用户");返回新资源<用户>(用户,linkToAll);}
为了进一步参考,以防其他人偶然发现这一点,我想通了:我在 application.properties
中添加了一个条目,即
spring.jackson.default-property-inclusion=NON_NULL
为什么这对于 Link
对象是必要的,但对于 User
我不知道(也没有深入研究).
I'm following a tutorial on Spring REST and am trying to add HATEOAS links to my Controller results.
I have a simple User class and a CRUD controller for it.
class User {
private int id;
private String name;
private LocalDate birthdate;
// and getters/setters
}
Service:
@Component
class UserService {
private static List<User> users = new ArrayList<>();
List<User> findAll() {
return Collections.unmodifiableList(users);
}
public Optional<User> findById(int id) {
return users.stream().filter(u -> u.getId() == id).findFirst();
}
// and add and delete methods of course, but not important here
}
Everything works fine except in my Controller, I want to add links from the all user list to the single users:
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users")
public List<Resource<User>> getAllUsers() {
List<Resource<User>> userResources = userService.findAll().stream()
.map(u -> new Resource<>(u, linkToSingleUser(u)))
.collect(Collectors.toList());
return userResources;
}
Link linkToSingleUser(User user) {
return linkTo(methodOn(UserController.class)
.getById(user.getId()))
.withSelfRel();
}
so that for every User in the result list, a link to the user itself is added.
The link itself is created fine, but there are superfluous entries in the resulting JSON:
[
{
"id": 1,
"name": "Adam",
"birthdate": "2018-04-02",
"links": [
{
"rel": "self",
"href": "http://localhost:8080/users/1",
"hreflang": null,
"media": null,
"title": null,
"type": null,
"deprecation": null
}
]
}
]
Where do the fields with null value (hreflang
, media
etc) come from and why are they added? Is there a way to get rid of them?
They do not appear when building a link to the all users list:
@GetMapping("/users/{id}")
public Resource<User> getById(@PathVariable("id") int id) {
final User user = userService.findById(id)
.orElseThrow(() -> new UserNotFoundException(id));
Link linkToAll = linkTo(methodOn(UserController.class)
.getAllUsers())
.withRel("all-users");
return new Resource<User>(user, linkToAll);
}
For further reference in case anyone else stumbles upon this and I figured it out: I added an entry to application.properties
, namely
spring.jackson.default-property-inclusion=NON_NULL
Why this was necessary for the Link
objects, but not for the User
I don't know (and haven't diven in deeper).
这篇关于Spring Hateoas ControllerLinkBuilder 添加空字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!