@Documented
@Target(value=CONSTRUCTOR)
@Retention(value=RUNTIME)
public @interface ConstructorProperties
示例
import com.fasterxml.jackson.databind.ObjectMapper;
import java.beans.ConstructorProperties;
public class ConstructorPropertiesAnnotationTest {
public static void main(String args[]) throws Exception {
ObjectMapper mapper = new ObjectMapper();
Employee emp = new Employee(115, "Raja");
String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(emp);
System.out.println(jsonString);
}
}
// Employee class
class Employee {
private final int id;
private final String name;
@ConstructorProperties({"id", "name"})
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public int getEmpId() {
return id;
}
public String getEmpName() {
return name;
}
}
登录后复制
输出
{
"empName" : "Raja",
"empId" : 115
}
登录后复制
以上就是在Java中使用Jackson时何时使用@ConstructorProperties注解?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!