gpt4 book ai didi

java - jackson 1.9.x 使用自定义注释进行反序列化

转载 作者:行者123 更新时间:2023-11-30 07:17:51 25 4
gpt4 key购买 nike

我有一个注释来指示 json 属性名称

public @interface ApiField {

/**json property name*/
public String value() default "";

}

像这样的类

public class User{
@ApiField("last_name")
String lastName;
public String getLastName(){...}

public void setLastName(String lastName){...}
}

json字符串是

{"last_name":"bob"}

How can I use this annotation to indicates property name instead of @JsonProperty ?

最佳答案

您可以使用 AnnotationIntrospector 来实现这一点

ApiAnnotationIntrospector

import java.lang.annotation.Annotation;

import org.codehaus.jackson.Version;
import org.codehaus.jackson.Versioned;
import org.codehaus.jackson.map.AnnotationIntrospector;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.KeyDeserializer;
import org.codehaus.jackson.map.annotate.JsonSerialize.Typing;
import org.codehaus.jackson.map.introspect.Annotated;
import org.codehaus.jackson.map.introspect.AnnotatedClass;
import org.codehaus.jackson.map.introspect.AnnotatedConstructor;
import org.codehaus.jackson.map.introspect.AnnotatedField;
import org.codehaus.jackson.map.introspect.AnnotatedMethod;
import org.codehaus.jackson.map.introspect.AnnotatedParameter;
import org.codehaus.jackson.type.JavaType;
import org.codehaus.jackson.util.VersionUtil;

public class ApiAnnotationIntrospector extends AnnotationIntrospector implements
Versioned {

@Override
public Version version() {
return VersionUtil.versionFor(getClass());
}

@Override
public boolean isHandled(Annotation ann) {
Class<?> cls = ann.annotationType();
if (ApiField.class == cls) {
return true;
}
return false;
}

@Override
public String findRootName(AnnotatedClass ac) {
return null;
}

@Override
public String[] findPropertiesToIgnore(AnnotatedClass ac) {
return null;
}

@Override
public Boolean findIgnoreUnknownProperties(AnnotatedClass ac) {
return null;
}

@Override
public boolean isIgnorableMethod(AnnotatedMethod m) {
return false;
}

@Override
public boolean isIgnorableConstructor(AnnotatedConstructor c) {
return false;
}

@Override
public boolean isIgnorableField(AnnotatedField f) {
return false;
}

@Override
public Object findSerializer(Annotated am) {
return null;
}

@Override
public Class<?> findSerializationType(Annotated a) {
return null;
}

@Override
public Typing findSerializationTyping(Annotated a) {
return null;
}

@Override
public Class<?>[] findSerializationViews(Annotated a) {
return null;
}

@Override
public String[] findSerializationPropertyOrder(AnnotatedClass ac) {
return null;
}

@Override
public Boolean findSerializationSortAlphabetically(AnnotatedClass ac) {
return null;
}

@Override
public String findGettablePropertyName(AnnotatedMethod am) {
return null;
}

@Override
public boolean hasAsValueAnnotation(AnnotatedMethod am) {
return false;
}

@Override
public String findEnumValue(Enum<?> value) {
return null;
}

@Override
public String findSerializablePropertyName(AnnotatedField af) {
return getPropertyName(af);
}

@Override
public Object findDeserializer(Annotated am) {
return null;
}

@Override
public Class<? extends KeyDeserializer> findKeyDeserializer(Annotated am) {
return null;
}

@Override
public Class<? extends JsonDeserializer<?>> findContentDeserializer(
Annotated am) {
return null;
}

@Override
public Class<?> findDeserializationType(Annotated am, JavaType baseType,
String propName) {
return null;
}

@Override
public Class<?> findDeserializationKeyType(Annotated am,
JavaType baseKeyType, String propName) {
return null;
}

@Override
public Class<?> findDeserializationContentType(Annotated am,
JavaType baseContentType, String propName) {
return null;
}

@Override
public String findSettablePropertyName(AnnotatedMethod am) {
return null;
}

@Override
public String findDeserializablePropertyName(AnnotatedField af) {
return getPropertyName(af);
}

@Override
public String findPropertyNameForParam(AnnotatedParameter param) {
return null;
}

private String getPropertyName(AnnotatedField af) {
ApiField annotation = af.getAnnotation(ApiField.class);
if (annotation != null && annotation.value() != "") {
return annotation.value();
}
return null;
}

}

然后如下更改您的 ObjectMapper

ObjectMapper mapper = new ObjectMapper();

AnnotationIntrospector primary = new ApiAnnotationIntrospector();
AnnotationIntrospector secondary = new JaxbAnnotationIntrospector();
AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary,
secondary);

mapper.getDeserializationConfig().setAnnotationIntrospector(pair);
mapper.getSerializationConfig().setAnnotationIntrospector(pair);

我的测试

public static class Type {
private String code;

private String description;

@ApiField("raw-value")
private String value;

public Type() {
}

public Type(String code, String description, String value) {
super();
this.code = code;
this.description = description;
this.value = value;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

}

和测试用例

Type t = new Type("c", "d", "e");
StringWriter sw = new StringWriter();
mapper.writeValue(sw, t);
System.out.println(sw.toString());

输出

{"code":"c","description":"d","raw-value":"e"}

关于java - jackson 1.9.x 使用自定义注释进行反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15784889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com