2017-03-06 95 views
0

嗨,我有以下類別:Java泛型和傑克遜映射

public class MyRequest { 
} 

public class MyResponse { 
} 

public class Request<T> { 
private String code; 
private T request; 

public setCode(String code) { 
    this.code = codel 
    } 

public setRequest(T request) { 
    this.request = request 
    } 
} 

和跟蹤服務請求的方法:

public MyResponse getMyResponse(Request<MyRequest> myRequest) { 
//process 
try { 
    ObjectMapper mapper = new ObjectMapper(); 
    String jsonInString = mapper.writeValueAsString(myRequest); 
    System.out.println(jsonInString); 
    } catch(Exception exp) {} 
} 

以下的Json請求是從JavaScript發送的;

{ 
    "code":"TESTCODE", 
    "request":null 
} 

我發送請求後,我得到一個無效的Json錯誤。任何人都可以告訴我什麼是我的請求錯誤JSON或其他錯誤..?

+0

如果你是積極的,通用實際上不是空,你的問題是反序列化。這將有助於:http://stackoverflow.com/questions/11664894/jackson-deserialize-using-generic-class – Eric

+0

其中是JavaScript的反序列化您的JSON的代碼? – Coder

+0

使用Firefox的HTTP請求附件發送此內容。這不是一個完整的代碼...以前的代碼沒有Java泛型是完美的工作..也是傑克遜用於春天.. – Adds

回答