java - Spring 和 Jackson Json : serialising two different sets of fields

标签 java spring spring-mvc jackson

我有一个 Classified 接口(interface),使用 @JsonAutoDetect 和 Visibility.NONE 进行注释,因此我可以选择要使用 @JsonSerialize 注释进行序列化的各个 getter

@JsonAutoDetect(getterVisibility = Visibility.NONE)
public interface Classified {

    @JsonSerialize
    String getModel();

直到这里都没有问题,当我从我的 @Controller 返回带有 @ResponseBody 注释的 Classified 时,它可以返回预期的 JSON:

    @RequestMapping(value = "/classified/{idClassified}", method = RequestMethod.GET)
    @ResponseBody
    public final Classified getClassified(@PathVariable final int idClassified) {

但是,当我返回分类列表时,我想返回较小的一组 getter,而使用以下签名,显然它会返回所有标记的 getter:

@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 子集。

最佳答案

查看“filtering properties ”,其中列出了更改序列化内容的多种方法。我猜 Json Views 可能是最简单的;可以使用一个较小的 View ,然后在未定义 View 时默认“全部”模式(默认是序列化所有属性)。

关于java - Spring 和 Jackson Json : serialising two different sets of fields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7578081/

相关文章:

java - Spring Boot 应用程序预填充数据出现错误

java - 在 ControllerAdvice 之前捕获反序列化异常

java - 将包含中文或日文或英文的字符串拆分为单词

java - 准备语句的空结果集

spring - 带有 Spring RequestMapping 路径参数的编码斜杠 (%2F) 提供 HTTP 400

mysql - Spring Boot、Spring Security、MySQL - CustomUserDetailsS​​ervice 始终导致错误 "Invalid Username or Password"

java - 从同一后端支持移动应用程序和网站的最佳技术堆栈是什么?

java - 如何在lucene中查询2个索引字段?

MySQL:错误 1215:无法添加外键约束

java - 将 Grails 集成到现有的 Spring 应用程序中?