Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
753 views
in Technique[技术] by (71.8m points)

java - Jackson's @JsonView annotation does not work

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

Here is my View class

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

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

  • 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);
    
  • fields, marked by @JsonView omitted in result if controller method annotated with OTHER @JsonView (see FilteredBeanPropertyWriter.serializeAsField)

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 comfasterxmljacksoncorejackson-databind2.8.4jackson-databind-2.8.4-sources.jar!comfasterxmljacksondatabindMapperFeature.java

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...