– Start
点击此处观看本系列配套视频。
如果你的类没有 setter 方法,我们也可以使用注解 @JsonCreator 修饰构造器。
package shangbo.jackson.demo15;
import org.apache.commons.lang3.builder.ToStringBuilder;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Person {
private String firstname;
private String lastname;
@JsonCreator
public Person(
@JsonProperty("firstname") String firstname,
@JsonProperty("lastname") String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
public String getFirstname() {
return firstname;
}
public String getLastname() {
return lastname;
}