Answers:
确切的操作方法取决于您使用的Jackson版本。围绕1.9版进行了更改,在此之前,您可以通过添加@JsonIgnore
到getter中来实现。
您尝试过的:
仅在getter方法上添加@JsonIgnore
执行此操作,然后将@JsonProperty
JSON“密码”字段名称的特定注释添加到对象密码的setter方法中。
杰克逊(Jackson)的最新版本已为添加READ_ONLY
和WRITE_ONLY
注释参数JsonProperty
。因此,您还可以执行以下操作:
@JsonProperty(access = Access.WRITE_ONLY)
private String password;
可以在这里找到文档。
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
为了做到这一点,我们需要做的是两个注释:
@JsonIgnore
@JsonProperty
使用@JsonIgnore
类成员和其吸气剂,并@JsonProperty
在它的制定者。示例示例将有助于实现此目的:
class User {
// More fields here
@JsonIgnore
private String password;
@JsonIgnore
public String getPassword() {
return password;
}
@JsonProperty
public void setPassword(final String password) {
this.password = password;
}
}
从2.6版开始:一种更直观的方法是com.fasterxml.jackson.annotation.JsonProperty
在字段上使用注释:
@JsonProperty(access = Access.WRITE_ONLY)
private String myField;
即使存在吸气剂,该字段值也会从序列化中排除。
JavaDoc说:
/**
* Access setting that means that the property may only be written (set)
* for deserialization,
* but will not be read (get) on serialization, that is, the value of the property
* is not included in serialization.
*/
WRITE_ONLY
如果您需要其他方法,请使用Access.READ_ONLY
。
就我而言,我让Jackson从Spring MVC控制器返回的对象自动(反序列化)对象(我在Spring 4.1.6中使用@RestController)。我不得不使用com.fasterxml.jackson.annotation.JsonIgnore
代替org.codehaus.jackson.annotate.JsonIgnore
,否则,它什么也没做。
"user": {
"firstName": "Musa",
"lastName": "Aliyev",
"email": "klaudi2012@gmail.com",
"passwordIn": "98989898", (or encoded version in front if we not using https)
"country": "Azeribaijan",
"phone": "+994707702747"
}
@CrossOrigin(methods=RequestMethod.POST)
@RequestMapping("/public/register")
public @ResponseBody MsgKit registerNewUsert(@RequestBody User u){
root.registerUser(u);
return new MsgKit("registered");
}
@Service
@Transactional
public class RootBsn {
@Autowired UserRepository userRepo;
public void registerUser(User u) throws Exception{
u.setPassword(u.getPasswordIn());
//Generate some salt and setPassword (encoded - salt+password)
User u=userRepo.save(u);
System.out.println("Registration information saved");
}
}
@Entity
@JsonIgnoreProperties({"recordDate","modificationDate","status","createdBy","modifiedBy","salt","password"})
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String country;
@Column(name="CREATED_BY")
private String createdBy;
private String email;
@Column(name="FIRST_NAME")
private String firstName;
@Column(name="LAST_LOGIN_DATE")
private Timestamp lastLoginDate;
@Column(name="LAST_NAME")
private String lastName;
@Column(name="MODIFICATION_DATE")
private Timestamp modificationDate;
@Column(name="MODIFIED_BY")
private String modifiedBy;
private String password;
@Transient
private String passwordIn;
private String phone;
@Column(name="RECORD_DATE")
private Timestamp recordDate;
private String salt;
private String status;
@Column(name="USER_STATUS")
private String userStatus;
public User() {
}
// getters and setters
}
解决此问题的另一种简便方法是allowSetters=true
在批注中使用参数。这将允许将密码反序列化为您的dto,但不会将其序列化为使用包含对象的响应主体。
例:
@JsonIgnoreProperties(allowSetters = true, value = {"bar"})
class Pojo{
String foo;
String bar;
}
两者foo
和bar
都填充在对象中,但是只有foo被写入响应主体。