spring - 使用 RestTemplae 获取 vavr Multimap 后的反序列化错误

标签 spring kotlin resttemplate vavr

我正在尝试反序列化 Jackson Multimap

       //given
        val newUser = NewUserDTO(ALREADY_TAKEN_USERNAME, ALREADY_TAKEN_EMAIL,"qddqwdW221QZ&^$!")

        //when
        val response = restTemplate.exchange("http://localhost:${port}/user", HttpMethod.POST, HttpEntity(newUser), typeReference())

        //then
        Assertions.assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, response.statusCode)

我得到一个异常(exception):
Type definition error: [simple type, class io.vavr.collection.Multimap]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `io.vavr.collection.Multimap` (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
 at [Source: (PushbackInputStream); line: 1, column: 1]
org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class io.vavr.collection.Multimap]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `io.vavr.collection.Multimap` (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
 at [Source: (PushbackInputStream); line: 1, column: 1]

有趣的是,如果我使用 String 来保存它的值并使用注入(inject)的 ObjectMapper,我可以反序列化响应
我该怎么办?

最佳答案

好吧,那是我的错。我用过TestRestTemplate,就像文档描述的那样,我应该自己配置那个bean:

If you need customizations (for example to adding additional message converters) use a RestTemplateBuilder @Bean.



我假设 RestTemplateBuilder 使用相同的 RestTemplate,因此如果为 RestTemplate 注册了一个模块,则应该为 RestTemplateBuilder 注册它

关于spring - 使用 RestTemplae 获取 vavr Multimap 后的反序列化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62161189/

相关文章:

java - 使用 RestTemplate 对 REST API 调用中的对象列表做出空响应

java - 无法使用log4j2配置spring cloud

java - 使用 Spring 从 JdbcTemplate 复制 sql 文件

java - 将 acegisecurity 框架与 spring 一起使用所需的示例链接

android - Android Kotlin将数据从Viewpager的基本 fragment 传递到其Tab fragment 之一

Kotlin:可见性修饰符更改匿名对象的类型

java - AbstractApplicationContext 尚未刷新异常

kotlin - 仅当右侧不为空时才分配的更好方法?

java - Spring RestTemplate 抛出 IllegalArgumentException : not a valid HTTP URL

java - 通过 JSON 中的 RestTemplate 发布请求