Spring和Jackson Json:序列化两组不同的领域 [英] Spring and Jackson Json: serialising two different sets of fields
问题描述
我有一个分类界面,带有Visibility.NONE的@JsonAutoDetect注释,所以我可以选择使用@JsonSerialize注释序列化的单个getter
I've a Classified interface, annotated with @JsonAutoDetect with Visibility.NONE, so I can pick individual getters to be serialized with the @JsonSerialize annotation
@JsonAutoDetect(getterVisibility = Visibility.NONE)
public interface Classified {
@JsonSerialize
String getModel();
直到这里没有问题,当我从@Controller返回带有@ResponseBody注释的分类,它可以返回预期的JSON:
Until here there is no problem, and when I return Classified with @ResponseBody annotation, from my @Controller, it works returning the expected JSON:
@RequestMapping(value = "/classified/{idClassified}", method = RequestMethod.GET)
@ResponseBody
public final Classified getClassified(@PathVariable final int idClassified) {
然而,当我返回分类列表时,我想返回一组较小的getter,而使用以下签名,显然它会返回所有标记的getter:
However when I return a List of Classifieds, I would like to return a smaller set of getters, while with the following signature, obviously it returns all marked getters:
@RequestMapping(value = "/classified", method = RequestMethod.GET)
@ResponseBody
public final List<Classified> searchClassified(@RequestParam final int idBrand,
@RequestParam final String priceMax, @RequestParam final int page) {
我不知道如何在列表的每个项目中返回较小的分类getter子集。
I don't know how to return a smaller subset of Classified getters in each item of the list.
推荐答案
返回较小的getter
"return a smaller set of getters"
如果你的意思是减少列表中的项目数,更改控制器的searchClassified方法中的业务逻辑。
If you mean reduce the number of items in the list, change the business logic in the controller's searchClassified method.
如果您的意思是减少每个项目上可用的公共getter方法的数量,您可以创建一个仅实现子集的接口原始项目获取者,并返回它们的列表。
If you mean reduce the number of available public getter methods on each item, you could create a interface that only implements a subset of the original items getters, and return a list of them instead.
这篇关于Spring和Jackson Json:序列化两组不同的领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!