简体   繁体   English

有没有办法发送地图 <String, CustomObject> 通过休息服务?

[英]Is there a way to send Map<String, CustomObject> via Rest service?

I have a server application where I have exposed some services. 我有一个服务器应用程序 ,我已经公开了一些服务。 I am running this application on Grizzly Jersey server. 我在Grizzly Jersey服务器上运行此应用程序。

I also have a client application wherein I call the services exposed by server application. 我还有一个客户端应用程序,其中我调用服务器应用程序公开的服务。 Again I am using the Jersey client to call the services. 我再次使用Jersey客户端来调用服务。

Now I have a requirement wherein I want to return: 现在我有一个要求,我想返回:

    Map<String, CustomObject>

from the rest service written in server application and I want other applications using my client to be able to retrieve the same map as is without going through any hassle. 从服务器应用程序中编写的其他服务,我希望使用我的客户端的其他应用程序能够检索相同的映射,而不会遇到任何麻烦。

Till now I have been passing CustomObject (s) from server application and my client deserializes the custom objects correctly. 直到现在我一直在从服务器应用程序传递CustomObject ,我的客户端正确地反序列化自定义对象。 This works because I have registered JacksonJaxbJsonProvider instance with my Jersey Client. 这是有效的,因为我已经使用Jersey客户端注册了JacksonJaxbJsonProvider实例。

I have already created a service which returns: 我已经创建了一个返回的服务:

    Map<String, CustomObject>

But the problem here is at the receiving end I get: 但这里的问题是在接收端我得到:

    Map<String, Map<String, String>>

Basically while returning a Map from service my CustomObject gets serialized into JSON which can be visualised as: 基本上,当从服务返回Map时 ,我的CustomObject被序列化为JSON,可以将其视为:

    Map<String, String>

but at the receiving end it does not get deserialized back to CustomObject. 但在接收端,它不会被反序列化回CustomObject。 Just to reiterate it works (deserializes) fine in cases where I return only CustomObject from service. 只是重申它在我从服务中只返回CustomObject的情况下工作(反序列化)很好。

I know I can traverse in main Map and convert internal Map into CustomObject using ObjectMapper at client side but that is something I want to avoid as there might be thousands of CustomObjects available. 我知道我可以遍历主Map并在客户端使用ObjectMapper将内部Map转换为CustomObject,但这是我想要避免的,因为可能有数千个CustomObjects可用。

Can someone please help here? 有人可以帮忙吗?

You have to declare the type of the destination object when deserializing your json. 在反序列化json时,必须声明目标对象的类型。 For example: 例如:

TypeReference<HashMap<String, CustomObject>> typeReference = new TypeReference<HashMap<String, CustomObject>>() {};
Map<String, CustomObject> result = mapper.readValue(jsonInput, typeReference);

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

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