使用@XmlEnumValue映射JAX-RS枚举

时间:2015-01-12 13:38:51

标签: enums jax-rs jboss-eap-6

我正在尝试映射这个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;
    }
}

2 个答案:

答案 0 :(得分:0)

我用那些丑陋的代码等待更好的答案:

@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);
    }
}

答案 1 :(得分:0)

老问题,但我会给出一个镜头......我没有使用XML注释类型,但我确实在将枚举转换为JSON方面遇到了同样的问题。

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

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