开发者

Spring and Jackson Json: serialising two different sets of fields

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();

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) {

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) {

I don't know how to return a smaller subset of Classified getters in each item of the list.


Check out "filtering properties", which lists multiple ways to change what gets serialized. I would guess that Json Views might be the easiest one; could use one smaller view, and then default "all" mode when no view is defined (default is then to serialize all properties).


"return a smaller set of getters"

If you mean reduce the number of items in the list, change the business logic in the controller's searchClassified method.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜