问题
I want to return different fields with different views for one object. But it always return all the fields. I created 3 different views:
public class Views {
public static class PublicView { }
public static class ExtendedPublicView extends PublicView { }
public static class InternalView extends ExtendedPublicView { }
}
Then in the User.java
@XmlRootElement()
public class User {
@JsonView(Views.PublicView.class)
private String username;
@JsonView(Views.PublicView.class)
private String employeeName;
@JsonView(Views.ExtendedPublicView.class)
private Date birthday;
@JsonView(Views.ExtendedPublicView.class)
private String mobile;
}
Then add @JsonView to
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
@JsonView(Views.PublicView.class)
public GenericResponse auth(@FormParam("username") String username, @FormParam("password") String password ){
}
回答1:
ok , i fix this several days ago. I check the source code of jackson I used that time , there were nothing doing about the @json view. That version I remember is 1.8.* Now I change to a newer one . it works
来源:https://stackoverflow.com/questions/17769101/how-can-a-jsonview-working-with-jersey