Jackson对于date的反序列化只支持几种,如果不符合默认格式则会报一下错误
org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.util.Date from String value '2012-12-12 12:01:01': not a valid representation (error: Can not parse date "2012-12-12 12:01:01": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
at [Source: org.glassfish.jersey.message.internal.EntityInputStream@2c384e19; line: 1, column: 2] (through reference chain: jerseyspring.representation.Order["payTime"])
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
解决方案
public class CustomJsonDateDeserializer extends JsonDeserializer<Date> {
@Override
public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = jp.getText();
try {
return format.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
且在字段的setter上加上注解
@JsonDeserialize(using = CustomJsonDateDeserializer.class)
SpringMVC使用jackson
public RequestMappingHandlerAdapter() {
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316
this.messageConverters = new ArrayList<HttpMessageConverter<?>>(4);
this.messageConverters.add(new ByteArrayHttpMessageConverter());
this.messageConverters.add(stringHttpMessageConverter);
this.messageConverters.add(new SourceHttpMessageConverter<Source>());
this.messageConverters.add(new AllEncompassingFormHttpMessageConverter());
}
```
```java
在public class AllEncompassingFormHttpMessageConverter extends FormHttpMessageConverter {
private static final boolean jaxb2Present =
ClassUtils.isPresent("javax.xml.bind.Binder", AllEncompassingFormHttpMessageConverter.class.getClassLoader());
private static final boolean jackson2Present =
ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", AllEncompassingFormHttpMessageConverter.class.getClassLoader()) &&
ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", AllEncompassingFormHttpMessageConverter.class.getClassLoader());
private static final boolean jackson2XmlPresent =
ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", AllEncompassingFormHttpMessageConverter.class.getClassLoader());
private static final boolean gsonPresent =
ClassUtils.isPresent("com.google.gson.Gson", AllEncompassingFormHttpMessageConverter.class.getClassLoader());
码片
org.codehaus.jackson.map.ObjectMapper jackson-mapper-asl
org.codehaus.jackson.JsonGenerator jackson-core-asl-1.9.2.jar
jar依赖
com.fasterxml.jackson.dataformat jackson-dataformat-xml 2.9.8xml的操作
XmlMapper mapper = new XmlMapper();
xml的配置
<beans:bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:bean
class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<beans:bean
class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
<beans:bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<beans:property name="objectMapper">
<beans:bean class="com.fasterxml.jackson.databind.ObjectMapper">
<beans:property name="dateFormat">
<beans:bean class="java.text.SimpleDateFormat">
<beans:constructor-arg type="java.lang.String" value="yyyy-MM-dd HH🇲🇲ss"/>
</beans:bean>
</beans:property>
</beans:bean>
</beans:property>
<beans:property name="supportedMediaTypes">
<beans:list>
<beans:value>application/json;charset=UTF-8</beans:value>
</beans:list>
</beans:property>
</beans:bean>
<beans:bean
class="org.springframework.http.converter.StringHttpMessageConverter">
<beans:constructor-arg value="UTF-8"></beans:constructor-arg>
<beans:property name="supportedMediaTypes">
<beans:list>
<beans:value>text/plain;charset=UTF-8</beans:value>
</beans:list>
</beans:property>
</beans:bean>
</beans:list>
</beans:property>
</beans:bean>