0
<student>
<id>1</id>
<badge></badge>
<name>Matt</name>
</student>

This is my xml post when I observe during post.

public class Student implements Serializable{

Long id;
Integer badge;
String name;
}

My Web Service :

    @Path("add")
    @POST
    @Consumes({Mediatype.xml,Mediatype.json})
    @Produces({Mediatype.xml,Mediatype.json})
    public Response add(Student student)
    {
    }

When I debug on this add method on my service layer , variable badge gets assigned value "0", even though it is not provided by GUI.Database field for badge is also integer(postgres).

Is it something related to Integer variable, which assigns default value to 0 if not present ?

Chetan D
  • 21
  • 4

1 Answers1

0

The type Integer is a reference type. When you create an instance of a class with a variable of a reference type, it's default value is null. So the behavior you are seeing is not Java's.

Jersey uses JAXB to marshall and unmarshall your entities. When it sees that you have a badge field that has an empty text element it must make a decision as to what the proper value to set is. It doesn't make sense to give it null since there is an element there. So it decides to initialize it to 0.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724