我在SpringMVC应用程序中使用MappingJacksonJsonView来从我的控制器渲染JSON。我希望我的对象中的ObjectId呈现为.toString,而是将ObjectId序列化为其部分。它在我的Velocity / JSP页面中运行得很好:
Velocity:
$thing.id
Produces:
4f1d77bb3a13870ff0783c25
Json:
<script type="text/javascript">
$.ajax({
type: 'GET',
url: '/things/show/4f1d77bb3a13870ff0783c25',
dataType: 'json',
success : function(data) {
alert(data);
}
});
</script>
Produces:
thing: {id:{time:1327331259000, new:false, machine:974358287, timeSecond:1327331259, inc:-260555739},…}
id: {time:1327331259000, new:false, machine:974358287, timeSecond:1327331259, inc:-260555739}
inc: -260555739
machine: 974358287
new: false
time: 1327331259000
timeSecond: 1327331259
name: "Stack Overflow"
XML:
<script type="text/javascript">
$.ajax({
type: 'GET',
url: '/things/show/4f1d77bb3a13870ff0783c25',
dataType: 'xml',
success : function(data) {
alert(data);
}
});
</script>
Produces:
<com.place.model.Thing>
<id>
<__time>1327331259</__time>
<__machine>974358287</__machine>
<__inc>-260555739</__inc>
<__new>false</__new>
</id>
<name>Stack Overflow</name>
</com.place.model.Thing>
有没有办法阻止MappingJacksonJsonView从ObjectId中获取那么多信息?我只想要.toString()方法,而不是所有细节。
感谢。
添加Spring配置:
@Configuration
@EnableWebMvc
public class MyConfiguration {
@Bean(name = "viewResolver")
public ContentNegotiatingViewResolver viewResolver() {
ContentNegotiatingViewResolver contentNegotiatingViewResolver = new ContentNegotiatingViewResolver();
contentNegotiatingViewResolver.setOrder(1);
contentNegotiatingViewResolver.setFavorPathExtension(true);
contentNegotiatingViewResolver.setFavorParameter(true);
contentNegotiatingViewResolver.setIgnoreAcceptHeader(false);
Map<String, String> mediaTypes = new HashMap<String, String>();
mediaTypes.put("json", "application/x-json");
mediaTypes.put("json", "text/json");
mediaTypes.put("json", "text/x-json");
mediaTypes.put("json", "application/json");
mediaTypes.put("xml", "text/xml");
mediaTypes.put("xml", "application/xml");
contentNegotiatingViewResolver.setMediaTypes(mediaTypes);
List<View> defaultViews = new ArrayList<View>();
defaultViews.add(xmlView());
defaultViews.add(jsonView());
contentNegotiatingViewResolver.setDefaultViews(defaultViews);
return contentNegotiatingViewResolver;
}
@Bean(name = "xStreamMarshaller")
public XStreamMarshaller xStreamMarshaller() {
return new XStreamMarshaller();
}
@Bean(name = "xmlView")
public MarshallingView xmlView() {
MarshallingView marshallingView = new MarshallingView(xStreamMarshaller());
marshallingView.setContentType("application/xml");
return marshallingView;
}
@Bean(name = "jsonView")
public MappingJacksonJsonView jsonView() {
MappingJacksonJsonView mappingJacksonJsonView = new MappingJacksonJsonView();
mappingJacksonJsonView.setContentType("application/json");
return mappingJacksonJsonView;
}
}
我的控制员:
@Controller
@RequestMapping(value = { "/things" })
public class ThingController {
@Autowired
private ThingRepository thingRepository;
@RequestMapping(value = { "/show/{thingId}" }, method = RequestMethod.GET)
public String show(@PathVariable ObjectId thingId, Model model) {
model.addAttribute("thing", thingRepository.findOne(thingId));
return "things/show";
}
}
答案 0 :(得分:1)
我不得不让getId()方法返回一个String。这是让杰克逊停止序列化ObjectId的唯一方法。
public String getId() {
if (id != null) {
return id.toString();
} else {
return null;
}
}
public void setId(ObjectId id) {
this.id = id;
}
setId()仍然必须是ObjectId,因此Mongo(及其驱动程序)可以正确设置ID。
答案 1 :(得分:1)
之前的回答确实很有效,但它很丑陋且没有经过深思熟虑 - 一个明确的解决方法来实际修复问题。
真正的问题是ObjectId
反序列化为其组成部分。 MappingJacksonJsonView
ObjectId
看到它是什么,一个对象,并开始研究它。在JSON中看到的反序列化字段是组成ObjectId
的字段。要停止此类对象的序列化/反序列化,您必须配置扩展ObjectMapper
的CustomObjectMapper
。
以下是CustomeObjectMapper
:
public class CustomObjectMapper extends ObjectMapper {
public CustomObjectMapper() {
CustomSerializerFactory sf = new CustomSerializerFactory();
sf.addSpecificMapping(ObjectId.class, new ObjectIdSerializer());
this.setSerializerFactory(sf);
}
}
以下是ObjectIdSerializer
使用的CustomObjectMapper
:
public class ObjectIdSerializer extends SerializerBase<ObjectId> {
protected ObjectIdSerializer(Class<ObjectId> t) {
super(t);
}
public ObjectIdSerializer() {
this(ObjectId.class);
}
@Override
public void serialize(ObjectId value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
jgen.writeString(value.toString());
}
}
以下是需要在@Configuration
- 注释类中更改的内容:
@Bean(name = "jsonView")
public MappingJacksonJsonView jsonView() {
final MappingJacksonJsonView mappingJacksonJsonView = new MappingJacksonJsonView();
mappingJacksonJsonView.setContentType("application/json");
mappingJacksonJsonView.setObjectMapper(new CustomObjectMapper());
return mappingJacksonJsonView;
}
您基本上是在告诉Jackson如何序列化/反序列化此特定对象。像魅力一样。
答案 2 :(得分:0)
默认情况下,Jackson提供接收到的对象的序列化。 ObjectId返回对象,因此其属性在转换为JSON后可见。您需要指定所需的序列化类型,在这种情况下,它是字符串。用于创建 ThingRepository 的 Thing 实体类将如下所示:
public class Thing {
@Id
@JsonSerialize(using= ToStringSerializer.class)
ObjectId id;
String name;
}
在这里记下添加的注释 @JsonSerialize(using = ToStringSerializer.class),该注释指示将ObjectID序列化为String。