@ConstructorProperties 注解來自 java.bean 包,用于通過帶注解的構(gòu)造函數(shù)將 JSON 反序列化為 java 對象。此注釋從Jackson 2.7版本開始支持。此注釋的工作方式非常簡單,我們可以提供一個包含每個構(gòu)造函數(shù)參數(shù)的屬性名稱的數(shù)組,而不是注釋構(gòu)造函數(shù)中的每個參數(shù)。
@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; <strong> </strong>@ConstructorProperties({"id", "name"})<strong> </strong> 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注解?的詳細(xì)內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!
java怎么學(xué)習(xí)?java怎么入門?java在哪學(xué)?java怎么學(xué)才快?不用擔(dān)心,這里為大家提供了java速學(xué)教程(入門到精通),有需要的小伙伴保存下載就能學(xué)習(xí)啦!
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號