2

There are already the same issue on this resource. But the answer seems totally wrong for me, and don't work for me.

What a problem:

com.fasterxml.jackson.databind.JsonMappingException: Could not resolve type id 'MyRequest$GetAll' into a subtype of [simple type, class Request]

What I had:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "@class")
@JsonSubTypes({ @JsonSubTypes.Type(value = MyRequest.GetAll.class)})
public class Request {
}

public class MyRequest extends Request {

    public static class GetAll extends MyRequest {
        public GetProfiles() {
    }
}

What I've tried to do:

I tried to follow that advice, and move subtypes registration from annotations to java code:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "@class")
public class Request {
}

public class MyRequest extends Request {

    public static class GetAll extends MyRequest {
        public GetProfiles() {
    }
}

public class Utils {
    private static final ObjectMapper mapper;

    static {
        mapper = new ObjectMapper();
        registerSubtypes();
        mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
        mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
    } 

    private static registerSubtypes() { //REGISTER ALL SUBTYPES
      mapper.registerSubtypes(MyRequest.GetAll.class);
      //...
    }   
}

But no result, I still get an exception! The most curious in this situation is that it works fine on my local machine, but doesn't work after deployment.

Question:

What I should try to solve this problem?

Community
  • 1
  • 1
VB_
  • 45,112
  • 42
  • 145
  • 293

1 Answers1

1

Not sure if makes a difference, but you may want to use @JsonTypeName on subtype, to explicitly name it, instead of relying on simple class name. This is safer to maintain, and less likely to cause issues if you refactor things (move class to different package for example).

StaxMan
  • 113,358
  • 34
  • 211
  • 239