0

Hi I have the following classes:

public class MyRequest {
}

and

public class MyResponse {
}

and

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

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

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

and following service request method:

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

and following Json request is sending from JavaScript;

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

After I send the request I an getting an Invalid Json error. Can anyone tell me what is wrong with my request Json or something else is wrong..?

Adds
  • 191
  • 3
  • 13
  • If you're positive that the generic isn't actually null, your problem is deserialization. This will help: http://stackoverflow.com/questions/11664894/jackson-deserialize-using-generic-class – Eric Mar 06 '17 at 19:04
  • where is the code which is deserializing your JSON from javascript? – Coder Mar 06 '17 at 19:11
  • Sending this using HTTP Request add-on of Firefox. This is not a full code... Previous code without Java generics was working perfectly.. Also Jackson is used on Spring side.. – Adds Mar 06 '17 at 19:29
  • The error doesn't seems to be related to the code here as you don't have JSON at all. You are trying to write the object as a string which will have JSON structure but that's not the problem here – Coder Mar 06 '17 at 20:58

1 Answers1

0

By any chance are you using the model 'Request' as an inner class. If yes,

  • just try using the modifier static with 'Request'.
  • or rather move out the model 'Request' to a separate class

Non-static Inner classes by design contain a reference to the outer-class, which does cause problems in deserialization.

http://www.cowtowncoder.com/blog/archives/2010/08/entry_411.html

Harshit
  • 1,174
  • 1
  • 9
  • 24