将Spring MVC @RequestBody转换为接口而不是类 [英] Spring MVC @RequestBody into an Interface instead of a class

查看:488
本文介绍了将Spring MVC @RequestBody转换为接口而不是类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计,

此代码剪辑有效:

@RequestMapping(method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<List<Member>> findByMemberKeyOrUniqueMemberId(

请注意,Member是一个接口,而不是一个类。

Note that "Member" is an interface, not a class.

这不起作用。

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<String> save(@RequestBody final List<Member> members) {

所以我可以将成员转换为JSON,但是我无法将JSON接收到成员中。如果我将保存的参数从List更改为其中一个成员类的列表,那么它可以正常工作,但它真的会弄乱我的代码。

So I can turn Members into JSON but I can't receive JSON into Members. If I change save's argument from List to a List of one of Member's classes then it works fine but it really messes up my code.

我该如何解决这个问题?

How can I fix this?

推荐答案

杰克逊不能(这是不可行的)猜哪个成员用于反序列化JSON的实现。

Jackson can not (it's infeasible) guess which Member implementation to use to deserialize the JSON.

Jackson提供 @JsonTypeInfo 作为一种描述你的继承层次结构的方法,并给出关于如何反序列化JSON的提示。但是,这需要你修改你的JSON并添加这些提示。这是exp在文档中,此处

Jackson offers @JsonTypeInfo as a way to describe your inheritance hierarchy and give it hints on how to deserialize JSON. However, this requires that you modify your JSON and add these hints. This is explained in the documentation, here.

这篇关于将Spring MVC @RequestBody转换为接口而不是类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆