English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

متى يجب استخدام علامة التبويب @ConstructorProperties في Jackson في Java؟

@ConstructorPropertiesتعليق منjava.beanحقيبة صغيرة تستخدم لتحويل JSON إلى أوبجكتات جافا من خلال التسلسل العكسي تعليق بناءهذا التعليق منإصدار Jackson 2.7يبدأ الدعم. طريقة عمل هذه التعليقات بسيطة للغاية، وليس من خلال تعليق كل متغير في صيغة البناء، يمكننا توفير اسم الخاصية لكل متغير في صيغة البناء.

النحو

@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);
   {}
{}
//طبقة الموظف
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
{}
أعجبك هذا