我试图使用spring数据elasticsearch为elasticsearch中的聚合创建一个rest api点。我能够在服务层获取数据,但当controller(即@RestController)试图将其返回给postman时,我收到了此错误。
无法写入JSON:对于输入字符串:“bHRMZzc5aHdodDF5a0hOck15Lzl1UT09”;嵌套异常com.fasterxml.jackson.databind.JsonMappingException:对于输入字符串:“bHRMZzc5aHdodDF5a0hOck15Lzl1UT09”(通过引用链:org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl[“聚合”]-
我的映射文件是这个
{
"transactions" : {
"properties" : {
"refernceId" : {
"type" :"text",
"index": true,
"store": true
},
"postingDate" : {
"type" : "date",
"index": true,
"store": true,
"format" : "yyyy-MM-dd'T'HH:mm:ss'Z'"
},
"effectiveDate" : {
"type" : "date",
"index": true,
"store": true,
"format" : "yyyy-MM-dd'T'HH:mm:ss'Z'"
},
"effectiveTime" : {
"type" : "text",
"index": true,
"store": true
},
"effectiveEpochTime" : {
"type" : "long",
"index": true,
"store": true
},
"transactionAmount" : {
"type" : "double",
"index": true,
"store": true
},
"transactionType" : {
"type" : "keyword",
"index": true,
"store": true
},
"transactionDesc" : {
"type" : "text",
"index": true,
"store": true
},
"transactionMemo" : {
"type" : "text",
"index": true,
"store": true
},
"transactionNumber" : {
"type" : "text",
"index": true,
"store": true
},
"transactionTypeCode" : {
"type" : "text",
"index": true,
"store": true
},
"transactionStatus" : {
"type" : "keyword",
"index": true,
"store": true
},
"principalAmount" : {
"type" : "double",
"index": true,
"store": true
},
"interest" : {
"type" : "text",
"index": true,
"store": true
},
"accountNumberToken" : {
"type" : "keyword",
"index": true,
"store": true
},
"memberNumberToken" : {
"type" : "keyword",
"index": true,
"store": true
},
"accountType" : {
"type" : "keyword",
"index": true,
"store": true
},
"userSub" : {
"type" : "keyword",
"index": true,
"store": true
},
"tenant" : {
"type" : "keyword",
"index": true,
"store": true
}
}
}
}
我的pom文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tyfone</groupId>
<artifactId>mcb-search-feature</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mcb-search-feature Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<jdk.version>1.8</jdk.version>
<spring.version>5.0.1.RELEASE</spring.version>
<jstl.version>1.2</jstl.version>
<servletapi.version>4.0.0</servletapi.version>
<spring.data.elastic.version>3.0.2.RELEASE</spring.data.elastic.version>
<slf4j.version>1.7.25</slf4j.version>
<junit.version>3.8.1</junit.version>
</properties>
<dependencies>
<!-- Spring MVC framework -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!-- for compile only, your container should have this -->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servletapi.version}</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-elasticsearch -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>${spring.data.elastic.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<!-- Testing pojo classes -->
<dependency>
<groupId>com.googlecode.openpojo</groupId>
<artifactId>openpojo</artifactId>
<version>0.6.0</version>
<scope>test</scope>
</dependency>
<!-- <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId>
<version>${junit.version}</version> <exclusions> <exclusion> <groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId> </exclusion> </exclusions> </dependency> -->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>mcb-search-feature</finalName>
</build>
</project>
对于任何在搜索后来到这里的人:我遇到了同样的问题,并使用了WebFlux。看起来它需要一个额外的配置。我合并了sung sung的代码,并在这里给出了答案:配置的ObjectMapper没有在spring boot webflux中使用
我完成的代码如下所示:
@Configuration
public class JacksonConfig{
@Bean
public Jackson2ObjectMapperBuilderCustomizer changeKeyAsNumber() {
return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.mixIn(ParsedStringTerms.ParsedBucket.class, MixIn.class);
}
@Bean
JavaTimeModule javaTimeModule(){
return new JavaTimeModule();
}
@Bean
public Jackson2JsonEncoder jackson2JsonEncoder(ObjectMapper objectMapper){
return new Jackson2JsonEncoder(objectMapper);
}
@Bean
public Jackson2JsonDecoder jackson2JsonDecoder(ObjectMapper objectMapper){
return new Jackson2JsonDecoder(objectMapper);
}
@Bean
public WebFluxConfigurer webFluxConfigurer(Jackson2JsonEncoder encoder, Jackson2JsonDecoder decoder){
return new WebFluxConfigurer() {
@Override
public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
configurer.defaultCodecs().jackson2JsonEncoder(encoder);
configurer.defaultCodecs().jackson2JsonDecoder(decoder);
}
};
}
}
abstract class MixIn {
@JsonIgnore
abstract public Number getKeyAsNumber();
}
我让它按照Amaresh Kulkarni建议使用ParsedString条款的方式工作。ParsedBucket.class而不是String条款。Bucket.class.这是完整的JacksonConfiguraton
@Configuration
public class JacksonConfig {
@Bean
public Jackson2ObjectMapperBuilderCustomizer changeKeyAsNumber() {
return new Jackson2ObjectMapperBuilderCustomizer() {
@Override
public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
jacksonObjectMapperBuilder.mixIn(ParsedStringTerms.ParsedBucket.class, MixIn.class);
}
};
}
}
abstract class MixIn {
@JsonIgnore
abstract public Number getKeyAsNumber();
}
如果有人仍然感兴趣。有完全相同的问题。这与Jackson并不真正知道AggregatedPage中包含的正确类型有关。因此,它只是遍历所有getter并在keyAsNumber上绊倒,其中键不是数字,在您的情况下为bHRMZzc5aHdodDF5a0hOck15Lzl1UT09。这最终会出现异常。
我最终在Spring应用程序启动期间配置了Jackson,以便在序列化期间省略keyAsNumber。
所以这就是我的工作:
@Configuration
public class JacksonConfig {
@Bean
public Jackson2ObjectMapperBuilderCustomizer changeKeyAsNumber() {
return new Jackson2ObjectMapperBuilderCustomizer() {
@Override
public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
jacksonObjectMapperBuilder.mixIn(StringTerms.Bucket.class, MixIn.class);
}
};
}
}
abstract class MixIn {
@JsonIgnore
abstract public Number getKeyAsNumber();
}
我正在尝试从gson迁移到jackson。这很令人沮丧,jackson在序列化方面做得更好(gson会因为各种原因阻塞jackson序列化的类而没有抱怨)然而,gson更擅长反序列化。 无法构造组织的实例。javautil。果心csv。SqlCsvExporterJson(尽管至少存在一个创建者):没有从字符串值反序列化的字符串参数构造函数/工厂方法。 我已经将其简化为一个简单的bean,我将其序
我有以下课 以及以下测试: 我收到以下错误: com.fasterxml.jackson.databind.exc.MismatchedInputException:无法构造com.store.domain.model.Cart实例(尽管至少存在一个Creator):无法从[Source:(String)"{"id":"56c7b5f7-115b-4cb9-9658-acb7b849d5d5"}"
我尝试调用一个url谁接受列表。 发送的数据为 “{”时间戳“:1445958336633,”状态“:400,”错误“:”错误请求“,”异常“:”org.springframework.http.converter.httpmessagenotreadableException“,”消息“:”无法读取文档:无法反序列化START_OBJECT令牌之外的java.util.arraylist实例\n
ClassCastException:com.google.gson.internal.LinkedTreeMap不能强制转换为java.util.HashMap 提前谢了。
我知道删除或修改注释可以做到这一点,但在这种情况下这不是一个选项。 -将关闭所有注释。这确实删除了冒犯字段,但也扼杀了我想工作的其他注释。
我将spring MVC用于服务。通过添加依赖项,我的rest控制器自动。 依赖关系 对象 问题 问题出现在中。例如,我想向添加。以下用户-JSON良好, 但是如果我添加一个带有子活动的activity,它就会崩溃! 的确,jackson这个json,我不明白为什么。我得到以下错误..知道吗? 这里的对象与所有字段,也许与我的错误有关?