1

I receiving an exception Could not resolve type id 'path.to.MyClass' as a subtype of [simple type, class java.lang.Object]: no such class found on play 2.7 server with DeadBolt (2.6.3 and 2.7.0) when I try deserialise JSON to Map<String, MyClass> inside of route action with a @Restrict annotation. All works fine if Remove this annotation.

MyClass.java


@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "class")
public class MyClass implements Serializable {
    public String name;
    public Integer age;
    public MyClass(){}
    public MyClass(String name, Integer age){
        this.name = name;
        this.age = age;
    }
}

serialise Map<String, MyClass>

Map<String, MyClass> value = new HashMap<>();
value.put("first", new MyClass("Bob",10));
value.put("second", new MyClass("Rob",20));

ObjectMapper mapper = Json.newDefaultMapper();
        mapper.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE, "class");
String json = null;
try {
    json = mapper.writeValueAsString(value);
} catch (JsonProcessingException e) {
    e.printStackTrace();
}

output

{
  "first":{
    "class":"path.to.MyClass",
    "name":"Bob",
    "age":10
  },
  "second":{
    "class":"path.to.MyClass",
    "name":"Rob",
    "age":20
  }
}

JSON format looks so because of backward compatibility with old server which use old FlexJson.

deserialise

@Restrict({@Group({"Admin"})})
public CompletionStage<Result> action(long id) {
    String json = getJsonFromStorage();
    Map<String, MyClass> result = new HashMap<>();
    try {
        ObjectMapper mapper = new ObjectMapper();
        mapper.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE, "class");
        JsonFactory factory = mapper.getFactory();
        JsonParser parser = factory.createParser(new ByteArrayInputStream(json.getBytes(Charset.forName("UTF-8"))));
        JavaType type = mapper.getTypeFactory().constructType(result.getClass());
        t = mapper.readValue(parser, type);
        } catch (IOException e) {
            e.printStackTrace();
        }

    return ok("ok")
}
Stepanov Andrey
  • 129
  • 2
  • 9

1 Answers1

0

I have a temporary solution. I override class loader for current thread to class loader from play.Environment

public class MyController extends Controller {

    @Inject
    private Environment environment;

    @Restrict({@Group({"Admin"})})
    public CompletionStage<Result> action(long id) {

        Thread.currentThread().setContextClassLoader(environment.classLoader());

        String json = getJsonFromStorage();
        Map<String, MyClass> result = new HashMap<>();
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE, "class");
            JsonFactory factory = mapper.getFactory();
            JsonParser parser = factory.createParser(new ByteArrayInputStream(json.getBytes(Charset.forName("UTF-8"))));
            JavaType type = mapper.getTypeFactory().constructType(result.getClass());
            t = mapper.readValue(parser, type);
            } catch (IOException e) {
                e.printStackTrace();
            }

        return ok("ok")
    }

}
Stepanov Andrey
  • 129
  • 2
  • 9