1

Could not resolve type id 'path.to.MyClass' as a subtype of [simple type, class java.lang.Object]: no such class found当我尝试Map<String, MyClass>使用 @Restrict 注释将 JSON 反序列化到路由操作内部时,我在使用 DeadBolt(2.6.3 和 2.7.0)的 play 2.7 服务器上收到异常。如果删除此注释,一切正常。

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

连载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();
}

输出

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

JSON 格式看起来如此,因为它向后兼容使用旧 FlexJson 的旧服务器。

反序列化

@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")
}
4

1 回答 1

0

我有一个临时解决方案。我将当前线程的类加载器从 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")
    }

}
于 2019-05-29T11:45:11.937 回答