【问题标题】:Deserialize two different JSON representations into one object将两种不同的 JSON 表示反序列化为一个对象
【发布时间】:2019-10-04 10:19:31
【问题描述】:

我有类似的 Java 类

@Data
public class Comment  {
  private Integer id; // should be used anyhow
  private Long refId; // for internal purpose -> not be serialized
  private String text; // should be used in QuickComment 
  private String patch; // should be included in PatchComment ONLY
  private String status; // should be included in StatusComment ONLY
}

我有

@Data
public class Response{
  private Comment statusComment;
  private Comment patchComment;
}

我想过用JsonViewlike

public class Views{
  public interface StatusComment{}
  public interface PatchComment{}
}

并将它们应用到初始类

@Data
public class Comment  {
  @JsonView({Views.StatusComment.class, Views.PatchComment.class})
  private Integer id; // should be used anyhow
  private Long refId; // for internal purpose -> not be serialized
  @JsonView({Views.StatusComment.class, Views.PatchComment.class})
  private String text; // should be used anyhow
  @JsonView(Views.PatchComment.class)
  private String patch; // should be included in PatchComment ONLY
  @JsonView(Views.StatusComment.class)
  private String status; // should be included in StatusComment ONLY
}

还有Response

@Data
public class Response{
  @JsonView(Views.StatusComment.class)
  private Comment statusComment;
  @JsonView(Views.PatchComment.class)
  private Comment patchComment;
}

但不知何故,它完全失败了。它完全失败了,即。什么都没有过滤。龙目岛有问题吗?还是定义不正确?

【问题讨论】:

    标签: java spring jackson lombok


    【解决方案1】:

    你如何序列化你的对象?你用的是弹簧吗?你是直接用ObjectMapper吗?

    如果您使用的是 Spring,那么您需要做的是使用 @JsonView(Views.StatusComment.class)@JsonView(Views.PatchComment.class) 注释控制器的方法,例如:

    用于读取GET 端点

    @JsonView(Views.StatusComment.class)
    @RequestMapping("/comments/{id}")
    public Comment getStatusComments(@PathVariable int id) {
        return statusService.getStatuscommentById(id);
    }
    

    写作:

    @RequestMapping(value = "/persons", consumes = APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    public Comment saveStatusComment(@JsonView(View.StatusComment.class) @RequestBody Comment c) {
        return statusService.saveStatusComment(c);
    }
    

    如果您直接使用ObjectMapper,那么您需要指定使用的View

    写作时:

    ObjectMapper mapper = new ObjectMapper();
    
    String result = mapper
        .writerWithView(Views.StatusComment.class)
        .writeValueAsString(comment);
    

    阅读时:

    ObjectMapper mapper = new ObjectMapper();
    Comment comment = mapper
        .readerWithView(Views.StatusComment.class)
        .forType(Comment.class)
        .readValue(json);
    

    【讨论】:

    • ahhhh... 但这意味着我没有得到一个同时包含两个条目的对象?例如return Response。条目内容不同。
    • Views 用于在不同的上下文中使用相同的对象。例如,您有一个对象的公共版本和一个仅供您自己查看的对象。然后你可以只使用一个类,但是根据使用哪个 View,你会看到不同的内容。
    • 啊……我明白了……以及如何为同一个对象获取两个不同版本的内容? (使用弹簧)
    • 我添加了一个例子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    • 2016-05-30
    • 2012-03-07
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多