Displaying a JAX-RS enum using @XmlEnumValue

I am trying to display this JSON:

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

      

With these JAX-RS classes:

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

      

I have this error (I am using 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)

      

After looking at the documentation , I also tried this definition without any success:

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

    private String code;

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

      

+3


source to share


2 answers


I used this ugly code expecting a better answer:



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

      

0


source


Old question, but I'll give this shot ... I am not using annotated XML types, but I had the same problem converting enums to JSON.

In my webapp, I already had a custom objectMapper to handle my use of JodaTime objects, so I added a serialize / deserialize function in the PostConstruct method.



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

      

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

0


source







All Articles