gpt4 book ai didi

spring - com.fasterxml.jackson.databind.exc.MismatchedInputException : Unexpected token

转载 作者:行者123 更新时间:2023-12-03 23:13:25 26 4
gpt4 key购买 nike

我正在尝试发布用户组:

public UserGroup createUserGroup(UserGroup userGroup) {
ResponseEntity<UserGroup> userGroupResponseEntity = oauthRestTemplate
.postForEntity(GROUPS_ENDPOINT, userGroup, UserGroup.class);
return userGroupResponseEntity.getBody();
}

因为我正在使用 @RepositoryRestResource我必须配置对象映射器:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new Jackson2HalModule());
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
messageConverter.setObjectMapper(objectMapper);
messageConverter.setSupportedMediaTypes(Collections.singletonList(MediaTypes.HAL_JSON));

this.oauthRestTemplate.setMessageConverters(Collections.singletonList(messageConverter));

但是,从上面运行 POST 会抛出我
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Unexpected token (START_OBJECT), expected VALUE_STRING: Expected array or string.
at [Source: (org.apache.catalina.connector.CoyoteInputStream); line: 1, column: 87] (through reference chain: mahlzeit.api.hibernate.model.UserGroup["voteUntil"])
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63) ~[jackson-databind-2.9.4.jar:2.9.4]
at com.fasterxml.jackson.databind.DeserializationContext.wrongTokenException(DeserializationContext.java:1507) ~[jackson-databind-2.9.4.jar:2.9.4]
at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:138) ~[jackson-datatype-jsr310-2.9.2.jar:2.9.2]
at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:39) ~[jackson-datatype-jsr310-2.9.2.jar:2.9.2]
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127) ~[jackson-databind-2.9.4.jar:2.9.4]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:369) ~[jackson-databind-2.9.4.jar:2.9.4]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159) ~[jackson-databind-2.9.4.jar:2.9.4]
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001) ~[jackson-databind-2.9.4.jar:2.9.4]
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3072) ~[jackson-databind-2.9.4.jar:2.9.4]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:235) ~[spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE]
... 93 common frames omitted

2018-04-02 15:12:52.788 DEBUG 1811 --- [nio-8081-exec-3] o.s.s.w.header.writers.HstsHeaderWriter : Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@24f17053
2018-04-02 15:12:52.789 DEBUG 1811 --- [nio-8081-exec-3] o.s.s.w.a.ExceptionTranslationFilter : Chain processed normally
2018-04-02 15:12:52.789 DEBUG 1811 --- [ main] o.s.s.oauth2.client.OAuth2RestTemplate : POST request for "http://localhost:8081/groups" resulted in 400 (null); invoking error handler
2018-04-02 15:12:52.789 DEBUG 1811 --- [nio-8081-exec-3] s.s.w.c.SecurityContextPersistenceFilter : SecurityContextHolder now cleared, as request processing completed
org.springframework.web.client.HttpClientErrorException: 400 null
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94)
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79)
at org.springframework.security.oauth2.client.http.OAuth2ErrorHandler.handleError(OAuth2ErrorHandler.java:172)
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:777)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:730)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.doExecute(OAuth2RestTemplate.java:128)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:686)
at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:468)

我就是不明白为什么!

最佳答案

对于遇到此问题的任何人 - 这就是为什么如果您不小心或记住您事先配置的一百万件事情,Spring 有时会变得如此令人沮丧。

所以如果你像我一样使用过 application-test.properties为了配置某些东西,让我们这样说:

# Used to enable ISO format for time data
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false

但是已经忘记了这一点,那么您可能要记住,一旦您像我一样设置自定义 ObjectMapper 后,该属性就会被忽略。为了消耗 hal+json Spring Data REST 端点的输出。正因为如此,您可能想要设置:
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.registerModule(new JavaTimeModule());

关于spring - com.fasterxml.jackson.databind.exc.MismatchedInputException : Unexpected token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49612046/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com