简体   繁体   English

Ajax与子类的Jackson编组调用(Spring MVC)

[英]Ajax call with Jackson marshaling of subclasses (Spring MVC)

I have a controller that receives an abstract class. 我有一个接收抽象类的控制器。 This class has 3 different subclasses. 此类具有3个不同的子类。

In order for the controller to receive the ajax call i must add a "@class" : com.my.project.my.Class parameter to the object i'm sending. 为了使控制器接收到ajax调用,我必须在要发送的对象上添加一个“ @class”:com.my.project.my.Class参数。

This feels really bad to implement. 实在很难实现。 Is there any other ways to send the subclasses and avoiding forcing the client to pass an actual class name with each call. 还有其他方法可以发送子类,并避免强迫客户端在每次调用时传递实际的类名。

Thanks 谢谢

Jackson has its own inheritance handling mechanism. 杰克逊有自己的继承处理机制。 You don't necessarily need to use @class . 您不一定需要使用@class See here . 看这里

This feature cannot be the cleanest. 此功能不是最干净的。 Jackson does its deserialization through hints from the developer. Jackson通过开发人员的提示进行反序列化。 Notice how you always pass a Class or TypeReference object when interacting with its ObjectMapper . 请注意,在与ClassTypeReference对象进行ObjectMapper交互时,始终如何传递它。 In the same way, the JSON needs to contain hints for which subtype to deserialize to. 以相同的方式,JSON需要包含反序列化到的子类型的提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM