Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java REST获取包含HashMap的对象的响应_Java_Android_Rest_Jackson - Fatal编程技术网

Java REST获取包含HashMap的对象的响应

Java REST获取包含HashMap的对象的响应,java,android,rest,jackson,Java,Android,Rest,Jackson,我有以下Rest服务,它查询数据库,构造多个“聊天”对象,并将它们作为数组返回: @GET @Path("/getChats") @Produces(MediaType.APPLICATION_JSON) public Chat[] getChats(@QueryParam("userId") String userId){ ArrayList<Chat> chats = getChatsDB(userId); Chat[] chatAr = new Chat[chats.

我有以下Rest服务,它查询数据库,构造多个“聊天”对象,并将它们作为数组返回:

@GET
@Path("/getChats")
@Produces(MediaType.APPLICATION_JSON)
public Chat[] getChats(@QueryParam("userId") String userId){

  ArrayList<Chat> chats = getChatsDB(userId);
  Chat[] chatAr = new Chat[chats.size()];
  return chats.toArray(chatAr);
}
@GET
@路径(“/getChats”)
@产生(MediaType.APPLICATION_JSON)
公共聊天[]getChats(@QueryParam(“userId”)字符串userId){
arraylistchats=getChatsDB(userId);
Chat[]chatAr=新聊天[chats.size()];
返回chats.toArray(chatAr);
}
“聊天”类是POJO:

import java.util.HashMap;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;


@JsonIgnoreProperties(ignoreUnknown = true)
public class Chat {
private String userId1;
private String userId2;
private HashMap<String, String> msgs;

public Chat() {
    msgs = new HashMap<>();
}

public String getUserId1() {
    return userId1;
}

public void setUserId1(String userId1) {
    this.userId1 = userId1;
}

public String getUserId2() {
    return userId2;
}

public void setUserId2(String userId2) {
    this.userId2 = userId2;
}
public void addMsg(String date, String msg){
    msgs.put(date, msg);
}

public HashMap<String, String> getMsgs() {
    return msgs;
}    
}
import java.util.HashMap;
导入com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown=true)
公共课堂聊天{
私有字符串userId1;
私有字符串userId2;
私有HashMap-msgs;
公共聊天室(){
msgs=newhashmap();
}
公共字符串getUserId1(){
返回userId1;
}
public void setUserId1(字符串userId1){
this.userId1=userId1;
}
公共字符串getUserId2(){
返回userId2;
}
公共void setUserId2(字符串userId2){
this.userId2=userId2;
}
public void addMsg(字符串日期,字符串消息){
msgs.put(日期,msg);
}
公共HashMap getMsgs(){
返回MSG;
}    
}
获取此聊天对象的客户端代码为:

public static Chat[] getChats() {
    Chat[] chats = null;
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    String chatUrl = url+"getChats?userId="+user.getId();
    chats = restTemplate.getForObject(chatUrl, Chat[].class);
    for(Chat c: chats){
        System.out.println(c.getUserId1());
        System.out.println(c.getUserId2());
        for(Map.Entry<String,String> e : c.getMsgs().entrySet()){
            System.out.println(e.getKey() + e.getValue());
        }
    }
    return chats;
publicstaticchat[]getChats(){
Chat[]chats=null;
RestTemplate RestTemplate=新RestTemplate();
restemplate.getMessageConverters().add(新映射Jackson2HttpMessageConverter());
字符串chatUrl=url+“getChats?userId=“+user.getId();
chats=restemplate.getForObject(chatUrl,Chat[].class);
用于(聊天室c:聊天室){
System.out.println(c.getUserId1());
System.out.println(c.getUserId2());
对于(Map.Entry e:c.getMsgs().entrySet()){
System.out.println(e.getKey()+e.getValue());
}
}
回访聊天;
客户端接收聊天对象,但没有包含消息的HashMap。c.getUserId1和c.getUserId2返回正确的值,但HashMap为空。此问题仅在客户端出现。servicemethod getChats(@QueryParam(“userId”)字符串userId)中的聊天对象在HashMap中具有正确的值

在浏览器中打开链接会显示以下内容:

[{“userId1”:“414”,“userId2”:“12”}]


对于内部映射,您需要在POJO中同时使用getter和setter

public class Chat {
    private HashMap<String, String> msgs;

    public void setMsgs(HashMap<String, String> msgs) {
        this.msgs = msgs;
    }    

    // rest of the code ...
}
您可能只需对客户端pojo进行如下更改即可解决此问题:

public void setMsgs(Map<String, List<Map<String,String>>> entries){
    for (Map<String, String> entry: entries.get("entry"))
        msgs.put(entry.get("key"),entry.get("value"));
}
public void setMsgs(地图条目){
for(映射条目:entries.get(“条目”))
msgs.put(entry.get(“key”)、entry.get(“value”);
}

对于内部映射,您需要在POJO中同时具有getter和setter

public class Chat {
    private HashMap<String, String> msgs;

    public void setMsgs(HashMap<String, String> msgs) {
        this.msgs = msgs;
    }    

    // rest of the code ...
}
您可能只需对客户端pojo进行如下更改即可解决此问题:

public void setMsgs(Map<String, List<Map<String,String>>> entries){
    for (Map<String, String> entry: entries.get("entry"))
        msgs.put(entry.get("key"),entry.get("value"));
}
public void setMsgs(地图条目){
for(映射条目:entries.get(“条目”))
msgs.put(entry.get(“key”)、entry.get(“value”);
}

Great,这会生成一个包含消息的有效json文件。但是客户端现在抛出一个异常:
org.springframework.http.converter.httpMessageNodeAbleException:无法读取json:无法反序列化位于的START\u数组令牌中的java.lang.String实例[来源:com.android.tools.profiler.support.network.HttpTracker]$InputStreamTracker@69c117a;行:1,列:11](通过引用链:Chat[“msgs”]);嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:无法反序列化START_数组令牌中的java.lang.String实例
@cyden,hm您是否可以控制服务器,输出看起来有点奇怪,当然您可以找到解析服务器输出的解决方案,但更改服务器可能更容易要为映射生成正确的json,请执行以下操作:
“maps”:{“key1”:“value1”}
而不是
“maps”:{“entry”:[{“key”:“key1”,“value”:“value1”}]}
更新我的答案时,只需对客户端进行更改即可解析您的答案data@cydennp,再看一看,我认为原因是db实际上给了POJO一个
List
Iterable
,而不是
Map
Great,这会生成一个包含消息的有效json文件。但是客户端现在抛出一个Exception:
org.springframework.http.converter.httpMessageNodeTableException:无法读取JSON:无法从[Source:com.android.tools.profiler.support.network.HttpTracker]的START_数组令牌中反序列化java.lang.String的实例$InputStreamTracker@69c117a;行:1,列:11](通过引用链:Chat[“msgs”]);嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:无法反序列化START_数组令牌中的java.lang.String实例
@cyden,hm您是否可以控制服务器,输出看起来有点奇怪,当然您可以找到解析服务器输出的解决方案,但更改服务器可能更容易要为映射生成正确的json,请执行以下操作:
“maps”:{“key1”:“value1”}
而不是
“maps”:{“entry”:[{“key”:“key1”,“value”:“value1”}]}
更新我的答案时,只需对客户端进行更改即可解析您的答案data@cydennp,再看一看,我认为原因是您的db实际上给您POJO的是
List
Iterable
而不是
Map