Java Spring 5.2.2版本无法连接到自定义消息转换器
我们的Spring项目最近从Spring 3.1.2升级到Spring 5.2.2,hibernet版本也升级到5.0.1.Final。和jackson库版本如下: 更新:Java Spring 5.2.2版本无法连接到自定义消息转换器,java,spring,jackson,Java,Spring,Jackson,我们的Spring项目最近从Spring 3.1.2升级到Spring 5.2.2,hibernet版本也升级到5.0.1.Final。和jackson库版本如下: 更新: jackson-annotations-2.9.0.jar jackson-core-2.9.4.jar jackson-core-asl-1.9.11.jar jackson-dataformat-cbor-2.9.6.jar jackson-databind-2.9.6.jar jackson-mapper-asl-1.
jackson-annotations-2.9.0.jar
jackson-core-2.9.4.jar
jackson-core-asl-1.9.11.jar
jackson-dataformat-cbor-2.9.6.jar
jackson-databind-2.9.6.jar
jackson-mapper-asl-1.9.11.jar
jackson-module-jaxb-annotations-2.0.1.jar
jackson-xc-1.9.11.jar
旧的:
下面是的servlet-context.xml代码:
`<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven>
<message-converters>
<beans:bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<beans:property name="supportedMediaTypes" value="application/json" />
<beans:property name="objectMapper">
<beans:bean class="com.appright.api.common.databind.JSONDateMapper" />
</beans:property>
</beans:bean>
</message-converters>
</annotation-driven>`
使用旧的jar,我们可以得到正确格式的响应,如“2020-07-23T19:49:54Z”。但是有了新的JAR和spring升级后,我们无法像旧的JAR那样获得响应
JSONDateMapper.java的代码:
公共类JSONDateMapper扩展了ObjectMapper{
/**
* Instantiates a new JSON date mapper.
*/
public JSONDateMapper() {
SimpleModule module = new SimpleModule("JSONModule", new Version(2, 0, 0, null, null, null));
module.addSerializer(DateTime.class, new DateSerializer());
module.addDeserializer(DateTime.class, new DateDeserializer());
// Add more here ...
registerModule(module);
}
}
DateSerializer.java的代码:
公共类DateSerializer扩展了StdSerializer{
/**
* Instantiates a new date serializer.
*/
public DateSerializer() {
super(DateTime.class);
}
/* (non-Javadoc)
* @see com.fasterxml.jackson.databind.ser.std.StdSerializer#serialize(java.lang.Object, com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.databind.SerializerProvider)
*/
@Override
public void serialize(DateTime date, JsonGenerator json, SerializerProvider provider) throws IOException, JsonGenerationException {
json.writeString(DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(DateTimeZone.UTC).print(date));
}
提前谢谢
/**
* Instantiates a new JSON date mapper.
*/
public JSONDateMapper() {
SimpleModule module = new SimpleModule("JSONModule", new Version(2, 0, 0, null, null, null));
module.addSerializer(DateTime.class, new DateSerializer());
module.addDeserializer(DateTime.class, new DateDeserializer());
// Add more here ...
registerModule(module);
}
/**
* Instantiates a new date serializer.
*/
public DateSerializer() {
super(DateTime.class);
}
/* (non-Javadoc)
* @see com.fasterxml.jackson.databind.ser.std.StdSerializer#serialize(java.lang.Object, com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.databind.SerializerProvider)
*/
@Override
public void serialize(DateTime date, JsonGenerator json, SerializerProvider provider) throws IOException, JsonGenerationException {
json.writeString(DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(DateTimeZone.UTC).print(date));
}