使用 @XmlEnumValue 映射 JAX-RS 枚举

Mapping a JAX-RS enum with @XmlEnumValue

我正在尝试映射这个 JSON :

{"ref":"code","type":"desc"}

使用那些 JAX-RS 类 :

public class Sorting {
    public String ref;
    public SortingType type;
}
@XmlType
@XmlEnum
public enum SortingType {
    @XmlEnumValue("asc")
    ASCENDING,
    @XmlEnumValue("desc")
    DESCENDING;
}

有了这个我有那个错误(我正在使用 JBoss EAP 6.2):

Caused by: org.codehaus.jackson.map.JsonMappingException: Can not construct instance of com.mycompany.myproject.SortingType from String value 'desc': value not one of declared Enum instance names
 at [Source: org.apache.catalina.connector.CoyoteInputStream@76e7449; line: 1, column: 47] (through reference chain: com.mycompany.myproject.Sorting["type"])
    at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)

通过查看 documentation,我也试过该定义但没有成功:

@XmlType
@XmlEnum
public enum SortingType {
    @XmlEnumValue("asc")
    ASCENDING("asc"),
    @XmlEnumValue("desc")
    DESCENDING("desc");

    private String code;

    SortingType(String code) {
    this.code = code;
    }
}

我使用了那个丑陋的代码等待更好的答案:

@XmlEnum
public enum SortingType {
    @XmlEnumValue("asc")
    ASCENDING,
    @XmlEnumValue("desc")
    DESCENDING;

    private static Map<String, SortingType> sortingTypeByValue = new HashMap<>();
    private static Map<SortingType, String> valueBySortingType = new HashMap<>();
    static {
        SortingType[] enumConstants = SortingType.class.getEnumConstants();
        for (SortingType sortingType : enumConstants) {
            try {
                String value = SortingType.class.getField(sortingType.name()).getAnnotation(XmlEnumValue.class).value();
                sortingTypeByValue.put(value, sortingType);
                valueBySortingType.put(sortingType, value);
            } catch (NoSuchFieldException e) {
                throw new IllegalStateException(e);
            }
        }
    }

    @JsonCreator
    public static SortingType create(String value) {
        return sortingTypeByValue.get(value);
    }

    @JsonValue
    public String getValue() {
        return valueBySortingType.get(this);
    }
}

老问题,但我会试一试...我没有使用 XML 注释类型,但我在将枚举转换为 JSON 时遇到了同样的问题。

在我的 webapp 中,我已经有一个自定义的 objectMapper 来处理我对 JodaTime 对象的使用,所以我在 PostConstruct 方法中添加了 serialization/deserialization 功能。

import javax.annotation.PostConstruct;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.joda.JodaModule;

@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper>{

    final ObjectMapper mapper = new ObjectMapper();

    public ObjectMapperContextResolver() {
        mapper.registerModule(new JodaModule());
    }

    @Override
    public ObjectMapper getContext(Class<?> type) {
        return mapper;
    }

    @PostConstruct
    public void customConfiguration(){
        mapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
        mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
    }
}

来源:http://fasterxml.github.io/jackson-databind/javadoc/2.3.0/com/fasterxml/jackson/databind/SerializationFeature.html