我的Jax RS REST API中有Java8 LocalDateTime。我的网络应用程序部署在wildfly10中。当我进行POST调用(其中包括LocalDateTime作为参数)时,我得到以下异常;
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class java.time.LocalDateTime] from String value ('2016-06-02T00:08:25.605Z'); no single-String constructor/factory method
at [Source: io.undertow.servlet.spec.ServletInputStreamImpl@396d1714; line: 2, column: 3] (through reference chain: com.leightonobrien.core.model.base.Company["created"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:843)
at com.fasterxml.jackson.databind.deser.ValueInstantiator._createFromStringFallbacks(ValueInstantiator.java:277)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:284)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1150)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:153)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:144)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:523)
at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:101)
at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.findDeserializeAndSet(BeanPropertyMap.java:285)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:248)
基于以下指南;
野蝇投掷";找不到接受javax.ws.rs.QueryParam的String param或valueOf()或fromString()方法的构造函数"错误
和
jaxrs找不到我的joda.money类型的自定义(反)序列化程序
和
https://github.com/FasterXML/jackson-datatype-jsr310
我已经编写了我的提供商并在应用程序路径中注册;
package com.test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;
import javax.ws.rs.ext.Provider;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
@Provider
public class LocalDateTimeConverterProvider extends JacksonJsonProvider implements ParamConverterProvider {
private final LocalDateTimeConverter converter = new LocalDateTimeConverter();
@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
if (!rawType.equals(LocalDateTime.class))
return null;
return (ParamConverter<T>) converter;
}
public class LocalDateTimeConverter implements ParamConverter<LocalDateTime> {
@Override
public LocalDateTime fromString(String value) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(value, formatter);
return dateTime;
}
@Override
public String toString(LocalDateTime value) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
String formattedDateTime = value.format(formatter);
return formattedDateTime;
}
}
public LocalDateTimeConverterProvider() {
ObjectMapper mapper = new ObjectMapper();
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.registerModule(new JavaTimeModule());
setMapper(mapper);
}
}
import javax.ws.rs.ApplicationPath;
@ApplicationPath("/rest")
public class RestApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
HashSet<Class<?>> set = new HashSet<Class<?>>();
set.add(com.test.JsonMoneyProvider.class);
set.add(com.test.DurtaionConverterProvider.class);
set.add(com.test.LocalDateTimeConverterProvider.class);
set.add(com.test.MoneyConverterProvider.class);
...
我打POST电话就像;
curl -X POST --header 'Content-Type: application/json' -d '{
"created": "2016-06-02T00:08:25.605Z",
"updated": "2016-06-02T00:08:25.605Z",
"id": 0,
"code": "string",
"name": "string",
"abn": "string",
"addresses": [
{
"id": 0,
"address1": "string",
"address2": "string",
"city": "string",
"state": "string",
"postcode": "string",
"country": "string",
"gps": {
"latitude": {
"latitude": 0,
"value": 0
},
"longitude": {
"longitude": 0,
"value": 0
}' 'http://localhost:8080/test2dbwar/rest/Companys'
我该如何克服上述问题?现在我一无所知。。我试着把所有的东西都放进去(尽量避免wildfly的jax-rs复杂支持问题,jackson序列化问题),并解决这个问题。。
有什么帮助吗?
链接到的第一个问题涉及@XxxParam
注释的转换。这就是ParamConverterProvider
的作用。这是一个与实体体(去)序列化完全不同的(去)串行化过程。
对于实体(反)序列化,使用MessageBodyReader/MessageBodyWriter
。Jackson在其JacksonJsonProvider/JacksonJaxbJsonProvider
中提供了一个这样的实现,无论您是否知道,您当前都在使用它。因此,LocalDataTime
的配置需要以某种方式使用该提供程序进行配置。
配置Jackson对LocalDateTime
序列化的支持的唯一方法是通过它的ObjectMapper
。您可以创建自定义Json(De)Serializer
,如本文所述(选项二),也可以使用本文所述的JSR310Module
(已经为LocalDateTime
创建了自定义Json(De)Serializer
)。
要将JacksonJsonProvider/JacksonJaxbJsonProvider
实际配置为使用您配置的ObjectMapper
,您可以使用ContextResolver
,如前面两个链接中所述,或者您可以用ObjectMapper
构造JacksonJsonProvider
,并注册该提供者
ObjectMapper mapper = new ObjectMapper();
mapper..configurModuleOrSerializer
JacksonJsonProvider provider = new JacksonJsonProvider(mapper);
registerProvier(provider)
就我个人而言,我会选择ContextResolver
,正如链接中提到的那样。不同的是,在上面的代码中,ObjectMapper
是显式提供给提供者的,而在ContextResolver
中,提供者将在运行时在JAX-RS注册表中搜索ObjectMapper
类型的ContextResolver
,然后以这种方式获得它。
在我的情况下,添加以下内容对我有效。
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.8.9</version
</dependency>
并像这样配置ObjectMapper
:
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());