【问题标题】:Jackson's @JsonView annotation does not work杰克逊的 @JsonView 注释不起作用
【发布时间】:2015-08-30 17:31:39
【问题描述】:

我用@JsonView 注释了用户类,当它返回时,我看到所有字段,甚至比视图类中不包含的字段。这是我的课

@Entity
@Table(name = "users")
public class User implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
private Long userID;
@JsonView(View.Summary.class)
@Column(name="email")
private String email;
@JsonView(View.Summary.class)
@Column(name="user_name")
private String firstName;
@JsonView(View.Summary.class)
@Column(name="user_last_name")
private String lastName;
@JsonView(View.Summary.class)
@Column(name="phone")
private String phone;
@JsonView(View.Summary.class)
@Column(name="origin")
private String address;
@JsonView(View.Summary.class)
@Column(name="birth_date")
private Long birthDate;
@JsonView(View.Summary.class)
@Column(name="gender")
private Long gender;
@JsonView(View.Summary.class)
@Column(name="about_me")
private String aboutMe;
@JsonView(View.SummaryWithPhoto.class)
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name="photo")
private Photo avatar;
@JsonView(View.SummaryWithSession.class)
@Transient
private UserSession session;

//getters and setters

这是我的视图类

public class View {
public interface Summary {}
public interface SummaryWithPhoto extends Summary {}
public interface SummaryWithSession extends SummaryWithPhoto {}
}

然后我请求带有@JsonView(View.SummaryWithPhoto.class) 注释的get 方法我总是得到userID 字段但不应该。这是端点代码

@JsonView(View.SummaryWithPhoto.class)
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<User> getUser(@RequestHeader(value="Access-key") String accessKey,
                                     @RequestHeader(value="Secret-key") String secretKey)

【问题讨论】:

  • 显示您的端点代码。
  • 你在哪个 Jackson 版本?
  • @AlexeyGavrilov 2.5.0

标签: java spring-mvc jackson


【解决方案1】:

我在同样的问题上花了一些调试时间。 结果是:

  • 如果您不更改此行为,则默认包含所有字段(请参阅BeanSerializerFactory.processViews)。要更改默认设置:

    ObjectMapper mapper = new ObjectMapper();
    mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
    
  • 如果控制器方法用 OTHER @JsonView 注释,则在结果中省略由 @JsonView 标记的字段(请参阅 FilteredBeanPropertyWriter.serializeAsField

因此,对于您的用例,请不要更改默认设置,将 Long userID 注释为 @JsonViewgetUser 注释为任何其他(不相同的)视图。

代码com\fasterxml\jackson\core\jackson-databind\2.8.4\jackson-databind-2.8.4-sources.jar!\com\fasterxml\jackson\databind\MapperFeature.java

   * Feature is enabled by default.
   */
   DEFAULT_VIEW_INCLUSION(true)

与博客https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring相矛盾,所以我得仔细看看代码。

【讨论】:

  • 很奇怪,因为早在 2014 年,Spring 博客文章就说反了:In Spring MVC default configuration, MapperFeature.DEFAULT_VIEW_INCLUSION is set to false. spring.io/blog/2014/12/02/…
  • 是的,完全正确!我在帖子中添加了 jackson MapperFeature 代码的片段。
  • 如果您自己创建 ObjectMapper,则 MapperFeature.DEFAULT_VIEW_INCLUSION 默认为 true。但是 spring 在 MappingJackson2HttpMessageConverter 中创建了自己的 ObjectMapper,并将功能设置为 false。
猜你喜欢
  • 2016-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-18
  • 1970-01-01
  • 1970-01-01
  • 2017-05-03
相关资源
最近更新 更多