package bj;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.List;
public class Foo {
public static void main(String[] args) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
Object o = objectMapper.readValue("[1,2,3]\n", objectMapper.getTypeFactory().constructParametricType(
List.class, Integer.class
));
System.out.println(o);
}
}
[1, 2, 3]
注意: Jackson
解析字符串要用方法com.fasterxml.jackson.databind.ObjectMapper#readValue(java.lang.String, com.fasterxml.jackson.databind.JavaType)
,而不是com.fasterxml.jackson.databind.ObjectMapper#convertValue(java.lang.Object, java.lang.Class<T>)
自定义一个org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer
的bean即可。
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
return jacksonObjectMapperBuilder ->
jacksonObjectMapperBuilder.featuresToDisable(
SerializationFeature.FAIL_ON_EMPTY_BEANS);
}