您好我试图将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集合,如import语句所示。我在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>
答案 0 :(得分:1)
我不熟悉SpringBoot,但您似乎应该在VavrModule
方法中返回ObjectMapper
对象而不是jacksonBuilder()
。
我基于这些链接:
答案 1 :(得分:1)
你注册为bean的ObjectMapper
不是用于序列化为JSON的那个,Spring MVC不会使用它,因为它不是他想要的东西。< / p>
要处理HTTP的内容Spring Spring MVC使用HttpMessageConverter,如果他在类路径中检测到Jackson,则MappingJackson2HttpMessageConverter将自动配置(如果未指定)。
在Spring Boot中添加应该足以注册自定义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
类中使用以下代码:
@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
希望它有所帮助!
答案 2 :(得分:1)
Spring Boot检索 com.fasterxml.jackson.databind.Module 类的所有实例,并使用它们初始化 ObjectMapper 。不需要额外的魔法。
我的依赖关系如下(Spring Boot 1.5.7.RELEASE):
/**
* Changes the appearance of the Menu widget on the page.
*/
function toggleMenu() {
var menu = app.currentPage.descendants.Menu1;
if (menu.styles.indexOf('hidden') < 0) {
menu.styles = ['hidden'];
} else {
menu.styles = [];
}
}
/**
* Forces closing of the Menu widget on the page.
*/
function closeMenu() {
var menu = app.currentPage.descendants.Menu1;
menu.styles = ['hidden'];
}
应用程序配置如下:
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");
}
}
您可以在此处查看代码:https://github.com/mihn/bootvavr