java Spring 3.2 和 Jackson 2:添加自定义对象映射器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14363555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring 3.2 and Hymanson 2: add custom object mapper
提问by alex
I'm developing a REST webservice in spring MVC. I need to change how Hymanson 2 serialize mongodb objectids. I'm not sure of what to do because I found partial documentation for Hymanson 2, what I did is to create a custom serializer:
我正在 Spring MVC 中开发 REST web 服务。我需要更改 Hymanson 2 序列化 mongodb objectids 的方式。我不确定该怎么做,因为我找到了 Hymanson 2 的部分文档,我所做的是创建一个自定义序列化程序:
public class ObjectIdSerializer extends JsonSerializer<ObjectId> {
@Override
public void serialize(ObjectId value, JsonGenerator jsonGen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
jsonGen.writeString(value.toString());
}
}
Create a ObjectMapper
创建一个对象映射器
public class CustomObjectMapper extends ObjectMapper {
public CustomObjectMapper() {
SimpleModule module = new SimpleModule("ObjectIdmodule");
module.addSerializer(ObjectId.class, new ObjectIdSerializer());
this.registerModule(module);
}
}
and then register the mapper
然后注册映射器
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean
class="org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter">
<property name="objectMapper">
<bean class="my.package.CustomObjectMapper"></bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
My CustomConverter is never called. I think the CustomObjectMapper definition is wrong,I adapted it from some code for Hymanson 1.x
我的 CustomConverter 从未被调用。我认为 CustomObjectMapper 定义是错误的,我根据 Hymanson 1.x 的一些代码对其进行了改编
In my controllers I'm using @ResponseBody. Where am I doing wrong? Thanks
在我的控制器中,我使用@ResponseBody。我哪里做错了?谢谢
回答by Taras
You should annotate corresponding model field with @JsonSerializeannontation. In your case it may be:
您应该使用@JsonSerialize注释来注释相应的模型字段。在您的情况下,它可能是:
public class MyMongoModel{
@JsonSerialize(using=ObjectIdSerializer.class)
private ObjectId id;
}
But in my opinion, it should better don't use entity models as VOs. Better way is to have different models and map between them. You can find my example project here(I used date serialization with Spring 3 and Hymanson 2 as example).
但在我看来,最好不要将实体模型用作 VO。更好的方法是拥有不同的模型并在它们之间进行映射。您可以在此处找到我的示例项目(我使用 Spring 3 和 Hymanson 2 的日期序列化作为示例)。
回答by user3614936
There is no need to create object mapper. Add Hymanson-core-2.0.0.jar and Hymanson-annotations-2.0.0.jar to your project.
无需创建对象映射器。将 Hymanson-core-2.0.0.jar 和 Hymanson-annotations-2.0.0.jar 添加到您的项目中。
Now, add the following lines of code to your controller while handing the service:
现在,在处理服务时将以下代码行添加到您的控制器中:
@RequestMapping(value = "students", method = RequestMethod.POST, headers = "Accept=application/json", consumes = "application/json")
public HashMap<String, String> postStudentForm(
@RequestBody Student student, HttpServletResponse response)
Do not miss any of the annotations.
不要错过任何注释。
回答by ninnemannk
How I would do this is:
我将如何做到这一点:
Create an annotation to declare your custom serializers:
创建一个注解来声明你的自定义序列化器:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyMessageConverter{
}
Set up component scan for this in your mvcconfiguration file
在您的 mvcconfiguration 文件中为此设置组件扫描
<context:include-filter expression="package.package.MyMessageConverter"
type="annotation" />
and create a class that implements HttpMessageConverter<T>
.
并创建一个实现HttpMessageConverter<T>
.
@MyMessageConverter
public MyConverter implements HttpMessageConverter<T>{
//do everything that's required for conversion.
}
Create a class that extends AnnotationMethodHandlerAdapter implements InitializingBean
.
创建一个类extends AnnotationMethodHandlerAdapter implements InitializingBean
。
public MyAnnotationHandler extends AnnotationMethodHandlerAdapter implements InitializingBean{
//Do the stuffs you need to configure the converters
//Scan for your beans that have your specific annotation
//get the list of already registered message converters
//I think the list may be immutable. So, create a new list, including all of the currently configured message converters and add your own.
//Then, set the list back into the "setMessageConverters" method.
}
I believe this is everything that is required for your goal.
我相信这是您的目标所需的一切。
Cheers.
干杯。