All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.swiftboot.web.util.JacksonUtils Maven / Gradle / Ivy

There is a newer version: 2.4.7
Show newest version
package org.swiftboot.web.util;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.swiftboot.util.AnnotationUtils;
import org.swiftboot.util.BeanUtils;

import java.lang.reflect.Field;

/**
 * @author swiftech
 **/
public class JacksonUtils {

    /**
     * 获取 bean 中指定名称 JsonProperty 注解的值
     *
     * @param targetBean
     * @param fieldName
     * @return
     */
    public static String getJsonPropertyValue(Object targetBean, String fieldName) {
        try {
            Field declaredField = BeanUtils.getDeclaredField(targetBean, fieldName);
            JsonProperty fieldAnnotation = (JsonProperty) AnnotationUtils.getFieldAnnotation(declaredField, JsonProperty.class);
            if (fieldAnnotation == null) {
                return fieldName;
            }
            else {
                return fieldAnnotation.value();
            }
        } catch (NoSuchFieldException e) {
            // 此处无需处理
        }
        return fieldName;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy