接口和@RequestBody [英] Interfaces and @RequestBody
问题描述
我目前正在开展一个项目,该项目允许用户在给定的时间段内(通过网络)预订所选资源的使用。在这个程序中,我试图遵循Spring的接口编程的哲学(以及通用的最佳实践),因此我尝试在具体类中重复功能的任何地方使用接口。
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.
我创建的一个接口称为BookableResourceController,它指定控制器处理所有要预订的资源所需的最低功能所需的方法。我还使用了第二个接口BookableResource,它识别哪些对象模拟了允许通过应用程序预订的资源。
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.
我目前遇到的问题是,BookableResourceController定义的一些方法使用@RequestBody映射将JSON对象转换为方法参数,并且自杰克逊以来只能将JSON转换为SimpleType对象,如果我将输入参数指定为BookableResource,则会收到错误。
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);
无法构造
org.codehaus.jackson的实例.map.type.SimpleType,
问题:抽象类型只能是
实例化,附加类型
信息
Can not construct instance of org.codehaus.jackson.map.type.SimpleType, problem: abstract types can only be instantiated with additional type information
据我所知,这个错误意味着我需要定义BookableResource的特定实现,这意味着我很可能需要从接口中排除这些方法,即使任何用于此目的的控制器都将需要那些方法。
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.
我要问的是,是否有人知道如何将接口定义为 @RequestBody <期望的对象/ code>使用JSON进行映射,或者是否有人建议如何构建我的控制器接口以包含这些方法?
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?
干杯
推荐答案
我不确定它会起作用,但你可以试着让它变得通用:
I'm not sure it would work, but you can try to make it generic:
public interface BookableResourceController<R extends BookableResource> {
@RequestMapping(value="/delete.html", method = RequestMethod.POST)
public ModelAndView processDeleteResource(@RequestBody R resource);
}
这篇关于接口和@RequestBody的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!