本文介绍了杰克逊的@JsonView注释不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I annotated class User with @JsonView and when it returned I see all fields even than that not contains in view class. Here is my class

@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

这是我的View类

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

然后我用<$ c请求get方法$ c> @JsonView(View.SummaryWithPhoto.class)注释我总是得到userID字段,但不应该。这是端点代码

SO then I request get method with @JsonView(View.SummaryWithPhoto.class) annotation I always get userID field but shouldn't. Here is endpoint code

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


推荐答案

我花了一些调试时间来处理同样的问题。
结果是:

I've spend a some debugging time with the same issue.Results are:


  • 如果不更改此行为,默认情况下会包含所有字段(请参阅 BeanSerializerFactory.processViews )。要更改默认值,请执行以下操作:

  • all fields are included by default if you do not change this behavior (see BeanSerializerFactory.processViews). To change default do:

ObjectMapper mapper = new ObjectMapper();
mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);


  • 字段,标记为 @JsonView 如果控制器方法使用OTHER @JsonView 注释,则在结果中省略(参见 FilteredBeanPropertyWriter.serializeAsField

  • fields, marked by @JsonView omitted in result if controller method annotated with OTHER @JsonView (see FilteredBeanPropertyWriter.serializeAsField)

    因此,对于您的用例,请不要更改默认设置,注释长用户ID by @JsonView getUser 由任何其他(不相同)视图。

    So for your use-case do not change default settings, annotate Long userID by @JsonView and getUser by any other (not the same) View.

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

    与blog ,所以我必须更仔细地看代码。

    is contradicted of blog https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring, so I have to look at code closer.

    这篇关于杰克逊的@JsonView注释不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 08-11 04:22