Spring Boot @JsonIgnore

时间:2018-04-16 18:14:53

标签: spring-boot

我可以在RestController方法中使用JsonIngore吗?如果我把@JsonIgnore放在我的VO中,他会在所有请求中忽略这个prop,但我只想在某些请求中忽略:sample:

我有

$('#base *').not('script, style').each(function () {
    currentElement = $(this);

    // Initialize on first loop.
    if (parentElementHorizontal === undefined && parentElementVertical === undefined) {
        parentElementHorizontal = currentElement;
        parentElementVertical = currentElement;
    }

    width = currentElement.outerWidth();
    height = currentElement.outerHeight();
    scrollWidthHidden = currentElement[0].scrollWidth;
    scrollHeightHidden = currentElement[0].scrollHeight;
    top = currentElement.offset().top;
    left = currentElement.offset().left;

    // Check if we're still within the parent containing horizontal-scrolling overflow.
    if (!$.contains(parentElementHorizontal[0], currentElement[0])) {
        hiddenWidth = false;
    }

    // Check if we're still within the parent containing vertical-scrolling overflow. 
    if (!$.contains(parentElementVertical[0], currentElement[0])) {
        hiddenHeight = false;
    }

    // Check if we've found an element with horizontal-scrolling content. 
    if (!hiddenWidth) {
        maxWidth = maxWidth < left + width ? left + width : maxWidth;
    } else if (currentElement.width() > maxWidth) {
        currentElement.addClass('redline-layer');
    }
    if (scrollWidthHidden > width && !hiddenWidth && width > 0) {
        hiddenWidth = true;
        parentElementHorizontal = currentElement;
    }

    // Check if we've found an element with vertical-scrolling content. 
    if (!hiddenHeight) {
        maxHeight = maxHeight < top + height ? top + height : maxHeight;
    } else if (currentElement.height() > maxHeight) {
        currentElement.addClass('redline-layer');
    }
    if (scrollHeightHidden > height && !hiddenHeight && height > 0) {
        hiddenHeight = true;
        parentElementVertical = currentElement;
    }
});

1 个答案:

答案 0 :(得分:1)

@JsonView是适合您的解决方案。 Here是一个示例,该链接的代码段如下所示

public class View {
    interface Summary {}
}

public class User {

    @JsonView(View.Summary.class)
    private Long id;

    @JsonView(View.Summary.class)
    private String firstname;

    @JsonView(View.Summary.class)
    private String lastname;

    private String email;
    private String address;
}


@RestController
public class MessageController {

    @Autowired
    private MessageService messageService;

    @JsonView(View.Summary.class)
    @RequestMapping("/")
    public List<Message> getAllMessages() {
        return messageService.getAll();
    }

    @RequestMapping("/{id}")
    public Message getMessage(@PathVariable Long id) {
        return messageService.get(id);
    }
}