Serializer/Deserializer 对于 Vavr 对象
Serializer/Deserializer for Vavr objects
你好,我正在尝试将 vavr 添加到我的项目中,现在我正在为 Vavr.List 对象的正确序列化而苦苦挣扎。下面是我的控制器:
import io.vavr.collection.List;
@GetMapping(value = "/xxx")
public List<EntityDeleted> getFile() {
return List.of(new EntityDeleted(true),new EntityDeleted(true),new EntityDeleted(true),new EntityDeleted(true));
}
EntityDeleted 是我的自定义对象,List 是 Vavr 集合,如导入语句所示。我在 Postman 中得到的响应是:
{
"empty": false,
"lazy": false,
"async": false,
"traversableAgain": true,
"sequential": true,
"singleValued": false,
"distinct": false,
"ordered": false,
"orNull": {
"deleted": true
},
"memoized": false
}
我期望 JSON 我的对象列表。以下是我的配置:
@SpringBootApplication
public class PlomberApplication {
public static void main(String[] args) {
SpringApplication.run(PlomberApplication.class, args);
}
@Bean
public ObjectMapper jacksonBuilder() {
ObjectMapper mapper = new ObjectMapper();
return mapper.registerModule(new VavrModule());
}
}
和pom.xml
的位
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr-jackson</artifactId>
<version>0.9.0</version>
</dependency>
我不熟悉 SpringBoot,但您似乎应该 return VavrModule
对象而不是 jacksonBuilder()
方法中的 ObjectMapper
。
我基于这些链接:
您注册为 bean 的 ObjectMapper
不是用于序列化为 JSON 的那个,Spring MVC 不会使用它,因为它不是他本身寻找。
为 HTTP Spring 处理内容 MVC 使用 HttpMessageConverter and if he detects Jackson in the classpath a MappingJackson2HttpMessageConverter 如果未指定,将自动配置。
在Spring启动添加应该足以注册自定义MessageConverter:
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new VavrModule());
// Spring MVC default Objectmapper configuration
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
jacksonConverter.setObjectMapper(mapper);
return jacksonConverter;
}
在纯 Spring MVC 应用程序中,您必须在 WebMvcConfigurerAdapter
class 中包含以下代码:
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new VavrModule());
// Spring MVC default Objectmapper configuration
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
jacksonConverter.setObjectMapper(mapper);
return jacksonConverter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(customJackson2HttpMessageConverter());
super.addDefaultHttpMessageConverters();
}
Some Documentation about ObjectMapper in Spring MVC
希望对您有所帮助!
Spring Boot 检索 com.fasterxml.jackson.databind.Module class 的所有实例并用它们初始化 ObjectMapper .不需要额外的魔法。
我的依赖如下(SpringBoot 1.5.7.RELEASE):
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile group: 'io.vavr', name: 'vavr', version: '0.9.1'
compile group: 'io.vavr', name: 'vavr-jackson', version: '0.9.1'
}
应用程序配置如下:
@SpringBootApplication
public class BootvavrApplication {
public static void main(String[] args) {
SpringApplication.run(BootvavrApplication.class, args);
}
@Bean
Module vavrModule() {
return new VavrModule();
}
}
控制器映射如下:
import io.vavr.collection.List;
@RestController
class TestController {
@GetMapping("/test")
List<String> testing() {
return List.of("test", "test2");
}
}
输出为:
["test","test2"]
您可以在此处查看代码:https://github.com/mihn/bootvavr
你好,我正在尝试将 vavr 添加到我的项目中,现在我正在为 Vavr.List 对象的正确序列化而苦苦挣扎。下面是我的控制器:
import io.vavr.collection.List;
@GetMapping(value = "/xxx")
public List<EntityDeleted> getFile() {
return List.of(new EntityDeleted(true),new EntityDeleted(true),new EntityDeleted(true),new EntityDeleted(true));
}
EntityDeleted 是我的自定义对象,List 是 Vavr 集合,如导入语句所示。我在 Postman 中得到的响应是:
{
"empty": false,
"lazy": false,
"async": false,
"traversableAgain": true,
"sequential": true,
"singleValued": false,
"distinct": false,
"ordered": false,
"orNull": {
"deleted": true
},
"memoized": false
}
我期望 JSON 我的对象列表。以下是我的配置:
@SpringBootApplication
public class PlomberApplication {
public static void main(String[] args) {
SpringApplication.run(PlomberApplication.class, args);
}
@Bean
public ObjectMapper jacksonBuilder() {
ObjectMapper mapper = new ObjectMapper();
return mapper.registerModule(new VavrModule());
}
}
和pom.xml
的位 <dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr-jackson</artifactId>
<version>0.9.0</version>
</dependency>
我不熟悉 SpringBoot,但您似乎应该 return VavrModule
对象而不是 jacksonBuilder()
方法中的 ObjectMapper
。
我基于这些链接:
您注册为 bean 的 ObjectMapper
不是用于序列化为 JSON 的那个,Spring MVC 不会使用它,因为它不是他本身寻找。
为 HTTP Spring 处理内容 MVC 使用 HttpMessageConverter and if he detects Jackson in the classpath a MappingJackson2HttpMessageConverter 如果未指定,将自动配置。
在Spring启动添加应该足以注册自定义MessageConverter:
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new VavrModule());
// Spring MVC default Objectmapper configuration
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
jacksonConverter.setObjectMapper(mapper);
return jacksonConverter;
}
在纯 Spring MVC 应用程序中,您必须在 WebMvcConfigurerAdapter
class 中包含以下代码:
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new VavrModule());
// Spring MVC default Objectmapper configuration
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
jacksonConverter.setObjectMapper(mapper);
return jacksonConverter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(customJackson2HttpMessageConverter());
super.addDefaultHttpMessageConverters();
}
Some Documentation about ObjectMapper in Spring MVC
希望对您有所帮助!
Spring Boot 检索 com.fasterxml.jackson.databind.Module class 的所有实例并用它们初始化 ObjectMapper .不需要额外的魔法。
我的依赖如下(SpringBoot 1.5.7.RELEASE):
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile group: 'io.vavr', name: 'vavr', version: '0.9.1'
compile group: 'io.vavr', name: 'vavr-jackson', version: '0.9.1'
}
应用程序配置如下:
@SpringBootApplication
public class BootvavrApplication {
public static void main(String[] args) {
SpringApplication.run(BootvavrApplication.class, args);
}
@Bean
Module vavrModule() {
return new VavrModule();
}
}
控制器映射如下:
import io.vavr.collection.List;
@RestController
class TestController {
@GetMapping("/test")
List<String> testing() {
return List.of("test", "test2");
}
}
输出为:
["test","test2"]
您可以在此处查看代码:https://github.com/mihn/bootvavr