概述
1.异常
Caused by: com.fasterxml.jackson.databind.JsonMappingException: (was java.sql.SQLFeatureNotSupportedException) (through reference chain: com.****.HttpResult["content"]->java.util.ArrayList[0]->java.util.LinkedHashMap["column_14"]->ru.yandex.clickhouse.ClickHouseArray["resultSet"])
使用jdbc操作clickhouse执行sql的返回结果时报错
2.背景知识
controller层使用@RestController时,使用MappingJackson2HttpMessageConverter对返回的数据进行序列化。所以前端可以显示json,xml等格式的数据。而@Controller注解显示的是视图,如果想展示json,要在方法中加上@ResponseBody
3.解决方案
项目中其他语句都没有报错,只有在返回 ClickHouseArray 这个类的时候序列化异常。没有看到其他解法,就自己写了序列化和反序列的代码。
代码如下:
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.TextNode;
import lombok.SneakyThrows;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import ru.yandex.clickhouse.ClickHouseArray;
import ru.yandex.clickhouse.domain.ClickHouseDataType;
import java.io.IOException;
import java.util.List;
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(mappingJackson2HttpMessageConverter());
WebMvcConfigurer.super.configureMessageConverters(converters);
}
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
//设置解析JSON工具类
ObjectMapper objectMapper = new ObjectMapper();
//解决空属性报错
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
//忽略未知属性 防止解析报错
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 全局clickhouseArray序列化配置
SimpleModule module = new SimpleModule();
//module.addDeserializer(ru.yandex.clickhouse.ClickHouseArray.class,new ClickHouseArrayDeserializer());
module.addSerializer(ru.yandex.clickhouse.ClickHouseArray.class,new ClickHouseArraySerialize());
objectMapper.registerModule(module);
objectMapper.findAndRegisterModules();
jsonConverter.setObjectMapper(objectMapper);
return jsonConverter;
}
public class ClickHouseArraySerialize extends JsonSerializer<ClickHouseArray> {
@SneakyThrows
@Override
public void serialize(ClickHouseArray clickHouseArray, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
// 对象转为字节流
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("elementType",clickHouseArray.getBaseTypeName());
jsonGenerator.writeObjectField("array",clickHouseArray.getArray());
jsonGenerator.writeEndObject();
}
}
}
最后
以上就是陶醉蜗牛为你收集整理的ClickHouseArray序列化异常的全部内容,希望文章能够帮你解决ClickHouseArray序列化异常所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复