我正在尝试POST一个用户组:
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)
我只是不明白为什么!
答案 0 :(得分:2)
对于遇到这种情况的任何人 - 这就是为什么如果你不小心或记住你事先配置的一百万件事情,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());