UXER UXER - 1 year ago 78
Java Question

Can field name be different from setter and getter in Jersey?

Hi I am new in Jersey and working on a piece of code, where the name of setter is defined differnt from its field name. I am confused if this is allowed and would actually work when sending and receiving json data over http. Please can someone please explain how getter and setter actually work in Jersey framework.

public class TestValue{
private String myLists;

public String getMyLists() {
return myLists;

public void setMyList(String myLists) {
this.myLists = myLists;


Answer Source

The default behavior is actually to look at the bean properties (the getters and setters). The field doesn't matter in this case. It is just a convention to have them named the same as the methods.

The getter is used for serialization (java to json). The get will be stripped from the method, and the first letter after is lowercased. This will be the resulting JSON property name.

getSomething()    =>  { "something": "value" }
getAnotherThing() =>  { "anotherThing": "value" }

Setters are used for deserialization (json to java). The set will be stripped, and the first letter after lowercased. This is the method name that will be looked up to match with the JSON property.

{ "something": "value" }  =>  setSomething(String somthing)

// The following leads to an error. It doen't follow the 
// casing rule where the first letter is lowercased
{ "Something": "value" }  =>  setSomething(String somthing)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download