2

I want to transfer an object with an additional integer to my Jersey-Service.

My service method looks like this:

@POST
@Path("/test")
@Consumes("application/json")
public void test(Person pers, int number)

and the Json in the body looks like this:

{
"name":"TEST",
"surname":"TEST",
"number":"1000"
}

It works without the additional integer.

So I think it must be a wrong JSON message?

BTW: the number is indepentent from the object, so I cannot integrate this var into the person-class.

Please can someone help me?

best regards

Oleksi
  • 12,947
  • 4
  • 56
  • 80
Igor
  • 33
  • 2
  • 6

1 Answers1

0

It will not work the way it appears in the question because Jersey does not serialize primitive types. check the following post: how-to-serialize-java-primitives-using-jersey-rest

It also does not support serializing the wrapper objects for the primitive types, in this example the Integer object. As the post above mentions, you can build your own class that wraps the int and has proper annotations.

for this case that you gave in the question, I think it would be enough to pass a String and then parse it for the int value:

@POST
@Path("/test")
@Consumes("application/json")
public void test(Person pers, String number){

    int x = Integer.parseInt(number);
    //rest of code here 
} 
Community
  • 1
  • 1
  • I changed it to string but its not working yet. I think there is a Problem with JSON-Format. I tried it like Gaby mentioned below but no success. { pers:{"name":"TEST", "surname":"TEST"}, "number":"1000" } Exception: Unexpected character ('p' (code 112)): was expecting double-quote to start field name – Igor Apr 25 '12 at 13:02
  • @Igor You should place in quotes the "pers" as well. – The Bitman May 18 '18 at 07:27