java - Json反序列化错误Spring Boot测试-无法构造实例

标签 java json spring spring-boot spring-cloud-stream

我有一个问题,下面是代码:-

MainService.java

public void someMethod(Message message){

A aObj = new A (someEnumClass.enumValue, message)

someService.saveData(aObj)

}

public Class A extends B {

int x,y,z;

A(SomeEnumClass enumValue,int c){

super(enumValue);

}

Public abstract class B<EnumClass extends Enum<EnumClass>>{

private EnumClass enumValue

}

现在上面的代码可以工作了:

但是当我使用下面的类进行测试时

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class MyDemoTests {
  @Autowired
    private SomeService someService;

    @Autowired
    private someRepo repository;


    @Test
        public void testMyservice() throws Exception {
            List<A> results = repository.findAll();
            assertThat(results).containsAll(all);

        }

我在“结果”上收到以下错误:-

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.lang.Enum` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
 at [Source: (InputStreamReader); line: 1, column: 123] (through reference chain: com.example.A["enumValue"])

非常感谢。谢谢

最佳答案

据我所知,您一定不能像您一样扩展Enum!唯一允许的方式是public enum ...。因此,不可能创建抽象枚举基类!

根据您真正想要做的事情,您也许可以使用包含默认方法的接口(interface)来实现相同的目的。枚举可以实现接口(interface)。

关于java - Json反序列化错误Spring Boot测试-无法构造实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54149383/

相关文章:

java - 运行 JUnit 测试时出错(JPA、Derby、Hibernate、Guice)

java - 400 在 Spring MVC 4 Rest 中转换 JSON 时出错

java - MVC 应用程序中的服务是否应返回实体或实体列表

spring - 从 springfox 迁移到 springdoc 时出现 Null ApiResponse

Spring Security Oauth - OAuth2Exceptions 的自定义格式

java - 发布并接收结果

java - Xcode支持Java程序编译和运行吗?

java - 为什么字符串 "getPriceDisplay"不会出现在我的 JTextField "totalTextField"中?

android - 如何在 Android 中使用 JSON Web 服务?

json - 将 PostgreSQL JSON 类型与 Slick 2 代码生成器一起使用