Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.AssertionError:无法构造java.lang.Enum的实例_Java_Jackson_Resteasy - Fatal编程技术网

java.lang.AssertionError:无法构造java.lang.Enum的实例

java.lang.AssertionError:无法构造java.lang.Enum的实例,java,jackson,resteasy,Java,Jackson,Resteasy,我正在开发一个客户机/服务器应用程序,在后端有一些使用RESTeasy开发的REST方法 特别是,有一个POST方法,作为输入,获取一个JSON字符串,然后将另一个JSON返回给调用客户端 从JSON到Java类的转换由Jackson以透明的方式执行,但我有一个问题: java.lang.AssertionError:无法构造java.lang.Enum的实例,问题:抽象类型只能使用其他类型信息进行实例化 由于此错误而失败的JUnit测试是: /** * Test for creation o

我正在开发一个客户机/服务器应用程序,在后端有一些使用RESTeasy开发的REST方法

特别是,有一个POST方法,作为输入,获取一个JSON字符串,然后将另一个JSON返回给调用客户端

从JSON到Java类的转换由Jackson以透明的方式执行,但我有一个问题:

java.lang.AssertionError:无法构造java.lang.Enum的实例,问题:抽象类型只能使用其他类型信息进行实例化

由于此错误而失败的JUnit测试是:

/**
 * Test for creation of json of {@link FindRequestBody}.
 */
@Test
public void testJackson2(){
    try {
        FindRequestBody findRequestBody = new FindRequestBody();        

        ObjectMapper jacksonMapper = new ObjectMapper();
        ObjectMapper mapper = new ObjectMapper();

        File file = new File("c:\\testFindRequestBody.json");
        jacksonMapper.writeValue(file, findRequestBody);

        Path path = Paths.get("c:\\testFindRequestBody.json");
        byte[] data = Files.readAllBytes(path);

        FindRequestBody returned = jacksonMapper.readValue(data, FindRequestBody.class);

        Assert.assertNotNull(returned);

    } 
    catch (JsonGenerationException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    } 
    catch (JsonMappingException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    } 
    catch (IOException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    } 
其余方法是:

 /**
 * {@inheritDoc}
 */
@POST
@Path("/filter")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Override
public List<Object> find(FindRequestBody findRequestBody){
    return service.find(findRequestBody);
}
有效字段是映射
枚举的字段。FieldRequestBody类包含OrderReadContext类,该类是:

public class OrderReadContext implements Serializable
{
    protected ValidOrAll valid = ValidOrAll.VALID_ONLY;

    public ValidOrAll getValid()
    {
        return this.valid;
    }

   public void setValid(ValidOrAll arg)
   {
        if (arg == null)
        {
            this.valid = ValidOrAll.VALID_ONLY;
        }
        else
        {
            this.valid = arg;
         }
    }
}
可能是因为在与JSON相对应的Java类中,有一些字段是
Enum


我无法使用批注,因此我想知道是否可以使用某些提供程序或拦截器等。

我已经解决了创建此类提供程序的问题:

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JacksonJsonProvider implements ContextResolver<ObjectMapper> {

    private ObjectMapper objectMapper;

    public JacksonJsonProvider() throws Exception {
        this.objectMapper = new ObjectMapper();

        System.out.println("JacksonJsonProvider():: Enter Into Constructor");        
        /*
         * First, you can globally declare that certain types always require additional type information:           
         * objectMapper.enableDefaultTyping(); // default to using DefaultTyping.OBJECT_AND_NON_CONCRETE
         * what this means is that for all types specified (for no-args, "Object.class" and all non-final classes), certain amount of default type information (Java class name, more specifically), is included, using default inclusion mechanism (additional wrapper array in JSON). This global default can be overridden by per-class annotations (more on this in next section).
         * 
         * The only thing you can configure, then, is just which types (classes) are affected. Choices are:
         * JAVA_LANG_OBJECT: only affects properties of type Object.class
         * OBJECT_AND_NON_CONCRETE: affects Object.class and all non-concrete types (abstract classes, interfaces)
         * NON_CONCRETE_AND+_ARRAYS: same as above, and all array types of the same (direct elements are non-concrete types or Object.class)
         * NON_FINAL: affects all types that are not declared 'final', and array types of non-final element types. 
         * This is often simplest initial way to enable enough type information to get things going. 
         */
         getObjectMapperForSerialization();

         System.out.println("JacksonJsonProvider():: Enabled Default Typing And Exit Constructor");
    }

    protected void getObjectMapperForSerialization() {
        System.out.println("JacksonJsonProvider():: Enter Into getObjectMapperForSerialization()");
        StdTypeResolverBuilder typeResolverBuilder = new ObjectMapper.DefaultTypeResolverBuilder(ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE);
        typeResolverBuilder = typeResolverBuilder.inclusion(JsonTypeInfo.As.PROPERTY);
        typeResolverBuilder.init(JsonTypeInfo.Id.CLASS, new ClassNameIdResolver(SimpleType.construct(Base.class), TypeFactory.defaultInstance()));
        this.objectMapper.setDefaultTyping(typeResolverBuilder);
    }

    public ObjectMapper getContext(Class<?> objectType) {
        return objectMapper;
    }
}
@Provider
@产生(MediaType.APPLICATION_JSON)
公共类JacksonJsonProvider实现ContextResolver{
私有对象映射器对象映射器;
公共JacksonJsonProvider()引发异常{
this.objectMapper=新的objectMapper();
System.out.println(“JacksonJsonProvider()::进入构造函数”);
/*
*首先,您可以全局声明某些类型始终需要其他类型信息:
*objectMapper.enableDefaultTyping();//默认使用DefaultTyping.OBJECT_和非_
*这意味着,对于所有指定的类型(对于无参数、“Object.class”和所有非final类),使用默认包含机制(JSON中的附加包装器数组)包含一定数量的默认类型信息(更具体地说,是Java类名)。这个全局默认值可以由每个类注释覆盖(下一节将对此进行详细介绍)。
* 
*那么,您唯一可以配置的就是哪些类型(类)受到影响。选项包括:
*JAVA_LANG_对象:仅影响OBJECT.class类型的属性
*对象和非具体:影响OBJECT.class和所有非具体类型(抽象类、接口)
*非具体数组和+\数组:同上,所有数组类型都相同(直接元素是非具体类型或Object.class)
*NON_FINAL:影响所有未声明为“FINAL”的类型,以及非FINAL元素类型的数组类型。
*这通常是启用足够的类型信息以使事情顺利进行的最简单的初始方法。
*/
getObjectMapperForSerialization();
System.out.println(“JacksonJsonProvider()::启用默认类型并退出构造函数”);
}
受保护的void getObjectMapperForSerialization(){
println(“JacksonJsonProvider()::进入getObjectMapperForSerialization()”;
stdtypesolverbuilder typeResolverBuilder=new ObjectMapper.DefaultTypeResolverBuilder(ObjectMapper.DefaultTyping.OBJECT_和_非_具体);
typeResolverBuilder=typeResolverBuilder.inclusion(JsonTypeInfo.As.PROPERTY);
typeResolverBuilder.init(JsonTypeInfo.Id.CLASS,新类名idResolver(SimpleType.construct(Base.CLASS),TypeFactory.defaultInstance());
this.objectMapper.setDefaultTyping(typeResolverBuilder);
}
公共对象映射器getContext(类objectType){
返回对象映射器;
}
}

并在resteasy提供者之间将其注册到web.xml中。

您可以发布一些代码吗?粗略猜测:您没有在服务器上使用Java 5或更高版本。要检查服务器上的Java版本:
Java-version
这里有一个快速提示-不要捕获测试用例中的所有异常,而是让测试方法抛出
Exception
并让所有错误向上传播-当测试失败时,您将获得更好的诊断。我的java版本是:“1.8.0_40”。我认为在一个异常块中捕获所有异常不是一个好主意,但除此之外,异常是明确的,这取决于Jackson在转换JSON->Java类时是否无法转换抽象字段。请发布
FindRequestBody
类和包含导致问题的
Enum
对象的类。
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JacksonJsonProvider implements ContextResolver<ObjectMapper> {

    private ObjectMapper objectMapper;

    public JacksonJsonProvider() throws Exception {
        this.objectMapper = new ObjectMapper();

        System.out.println("JacksonJsonProvider():: Enter Into Constructor");        
        /*
         * First, you can globally declare that certain types always require additional type information:           
         * objectMapper.enableDefaultTyping(); // default to using DefaultTyping.OBJECT_AND_NON_CONCRETE
         * what this means is that for all types specified (for no-args, "Object.class" and all non-final classes), certain amount of default type information (Java class name, more specifically), is included, using default inclusion mechanism (additional wrapper array in JSON). This global default can be overridden by per-class annotations (more on this in next section).
         * 
         * The only thing you can configure, then, is just which types (classes) are affected. Choices are:
         * JAVA_LANG_OBJECT: only affects properties of type Object.class
         * OBJECT_AND_NON_CONCRETE: affects Object.class and all non-concrete types (abstract classes, interfaces)
         * NON_CONCRETE_AND+_ARRAYS: same as above, and all array types of the same (direct elements are non-concrete types or Object.class)
         * NON_FINAL: affects all types that are not declared 'final', and array types of non-final element types. 
         * This is often simplest initial way to enable enough type information to get things going. 
         */
         getObjectMapperForSerialization();

         System.out.println("JacksonJsonProvider():: Enabled Default Typing And Exit Constructor");
    }

    protected void getObjectMapperForSerialization() {
        System.out.println("JacksonJsonProvider():: Enter Into getObjectMapperForSerialization()");
        StdTypeResolverBuilder typeResolverBuilder = new ObjectMapper.DefaultTypeResolverBuilder(ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE);
        typeResolverBuilder = typeResolverBuilder.inclusion(JsonTypeInfo.As.PROPERTY);
        typeResolverBuilder.init(JsonTypeInfo.Id.CLASS, new ClassNameIdResolver(SimpleType.construct(Base.class), TypeFactory.defaultInstance()));
        this.objectMapper.setDefaultTyping(typeResolverBuilder);
    }

    public ObjectMapper getContext(Class<?> objectType) {
        return objectMapper;
    }
}