Jackson Error Java EnumMap

529 Views Asked by At

I am trying to deserialize an EnumMap with Jackson, and I am running into the following error:

javax.ws.rs.client.ResponseProcessingException: com.fasterxml.jackson.databind.JsonMappingException: Can not construct EnumMap; generic (key) type not available
 at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@793ff30f; line: 1, column: 1]

Here's the enum I'm using for the keys:

public enum ResultTypeEnum
{
    PRESETS,
    SUMMARY,
    TERMINATION,
    TIMESERIES
}

The server code is this:

@GET
    @Path("/results")
    @Produces(MediaType.APPLICATION_JSON)
    public DiagnoticResults getAllResults() {
        try {
            return subjectService.onGetAllResults();

        } catch (IllegalOperationException ex) {
            throw new InternalServerErrorException(ex);
        }
    }

The client code is this:

@SuppressWarnings("unchecked")
    public EnumMap getAllResults() {
        return client.target(subjectURI + "results").request(MediaType.APPLICATION_JSON)
                .get(EnumMap.class);

    }

The diagnostic results class is this:

public class DiagnoticResults extends EnumMap<ResultTypeEnum, byte[]> {

    public DiagnoticResults() {
        super(ResultTypeEnum.class);
    }
}

This is a class I wrote from another user that was having issues with an EnumMap.

1

There are 1 best solutions below

0
On

I was not able to reproduce your problem, so I'm kind of guessing here.

It seems the client-code has problems in creating an EnumMap<K, V> by calling the constructor EnumMap(Class<K>), because there is no information about which generic type K type to use.

Therefore, in your client-code you should replace EnumMap by DiagnoticResults.
Instead of your code

@SuppressWarnings("unchecked")
public EnumMap getAllResults() {
    return client.target(subjectURI + "results").request(MediaType.APPLICATION_JSON)
            .get(EnumMap.class);
}

try this code:

public DiagnoticResults getAllResults() {
    return client.target(subjectURI + "results").request(MediaType.APPLICATION_JSON)
            .get(DiagnoticResults.class);
}