接口和@RequestBody
我目前正在开发一个项目,该项目允许用户(通过网络)预订在给定时间段内使用所选资源。在这个程序中,我试图遵循 Spring 的接口编程哲学(以及一般的最佳实践),因此我尝试在具体类中重复功能的任何地方使用接口。
我创建的一个接口称为 BookableResourceController,它指定控制器处理要预订的任何类型资源所需的最低功能所需的方法。我还使用了第二个接口 BookableResource,它标识哪些对象模拟了允许通过应用程序预订的资源。
我当前遇到的问题是 BookableResourceController 定义的一些方法使用 @RequestBody 映射将 JSON 对象转换为方法参数,并且由于 Jackson 只能将 JSON 转换为“SimpleType”对象,因此如果我将输入参数指定为 BookableResource。
@RequestMapping(value="/delete.html", method = RequestMethod.POST)
public ModelAndView processDeleteResource(
@RequestBody BookableResource resource);
无法构造实例 org.codehaus.jackson.map.type.SimpleType, 问题:抽象类型只能是 用附加类型实例化 信息
据我所知,此错误意味着我需要定义 BookableResource 的特定实现,这意味着我很可能需要从接口中排除这些方法,即使用于此目的的任何控制器都需要这些方法。
我要问的是,是否有人知道如何将接口定义为使用 JSON 的 @RequestBody
映射所期望的对象,或者是否有人对如何按顺序构建我的控制器接口有任何建议包括这些方法?
干杯
I'm currently working on a project which allows users to book (via the web) the use of a chosen resource for a given period of time. In this program I am trying to keep with Spring's philosophy (and the general best practice) of programming to interfaces and as such I try to use interfaces anywhere where functionality is repeated among concrete classes.
One interface I have created is called a BookableResourceController which specifies the methods needed by a controller to handle the minimum required functionality for any type of resource to be booked. I also make use of a second interface, BookableResource, which identifies which objects model a resource that is allowed to be booked through the application.
The problem I am currently running into is that a few of the methods defined by BookableResourceController use the @RequestBody mapping to convert a JSON object into a method parameter, and since Jackson can only convert JSON into "SimpleType" objects, I receive an error if I specify the input parameter to be a BookableResource.
@RequestMapping(value="/delete.html", method = RequestMethod.POST)
public ModelAndView processDeleteResource(
@RequestBody BookableResource resource);
Can not construct instance of
org.codehaus.jackson.map.type.SimpleType,
problem: abstract types can only be
instantiated with additional type
information
From what I can tell this error means that I will need to define a specific implementation of BookableResource, meaning I will most likely need to exclude these methods from the interface even though any controller that is to be used for this purpose will require those methods.
What I am asking is if anyone knows a way to define an interface as the object that is expected from an @RequestBody
mapping using JSON, or does anyone have any suggestions of how to structure my contoller interface in order to include these methods?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定它是否有效,但你可以尝试使其通用:
I'm not sure it would work, but you can try to make it generic:
可以使用的其他方法是使用 Jackson 注释来注释接口:(
如果不想直接在接口类中添加它,可以使用混合注释)
编辑:另一种可能性是使用
SimpleModule
方法addAbstractTypeMapping()
指定实现类型。这避免了从接口到实现的链接,并且可能(或可能不是)注册此方面的更方便的方式。Additional way to go that can be used is to annotate interface with Jackson annotation:
(possibly using mix-in annotations if one does not want to add it directly in interface class)
EDIT: Another possibility is to use
SimpleModule
methodaddAbstractTypeMapping()
to specify implementation type. This avoids linkage from interface to implementation, and may (or may not) be more convenient way to register this aspect.