0

I have a web application. The front-end is in ember.js and the back-end is in Java, Spring MVC. I am facing a very weird problem.

I make an ajax request to my back-end and fetch some data. I return a custom object.

When I fetch data from the back-end on my local machine, all is fine but on a local server the custom object excludes Boolean fields. (Rest fields like String are received fine).

In the class of my custom object, I have set the @JsonProperty annotations on the getters and setters of these Boolean variables.

In my javascript file, I am sending some params in an ajax request via GET with contentType: "application/json"

This the controller:

@ResponseBody
@RequestMapping(value = "/getData", method = RequestMethod.GET,
                        produces = MediaType.APPLICATION_JSON_VALUE)
public MyCustomObject(@RequestParam(value = "name") String name) {
    MyCustomObject obj = null;
    if(name != null) {
        obj = fetchDataByName(name);
        // obj.getInList() = false and obj.getIsParent() = false
        // Values were properly initialized
    }

    return null;
}

I am 100% sure that the code is the same both on server and my local environment, and the data has those boolean variables before sending out the custom object.

I can't figure out what might be wrong here, am I missing some configuration? Or is there something else?

UPDATED:

public class MyCustomObject {
    private ID id;
    private Name name;
    private InList inList;
    private IsParent isParent;  

    @JsonProperty
    public ID getID() { return this.id;}

    @JsonProperty
    public void setID(ID id) { this.id = id;}

    @JsonProperty
    public Name getName() { return this.name;}

    @JsonProperty
    public void setName(Name name) { this.name = name;}

    @JsonProperty
    public InList getInList() { return this.inList;}

    @JsonProperty
    public void setInList(InList inList) { this.inList = inList;}

    @JsonProperty
    public IsParent getIsParent() { return this.isParent;}

    @JsonProperty
    public void setIsParent(IsParent isParent) { this.isParent = isParent;}
}

public class ID {
    String data;

    @JsonProperty
    public String getData() { return this.data;}

    @JsonProperty
    public void setData(String data) { this.data = data;}
}

public class Name {
    String data;

    @JsonProperty
    public String getData() { return this.data;}

    @JsonProperty
    public void setData(String data) { this.data = data;}
}

public class InList {
    Boolean data;

    @JsonProperty
    public Boolean getData() { return this.data;}

    @JsonProperty
    public void setData(Boolean data) { this.data = data;}
}

public class IsParent {
    Boolean data;

    @JsonProperty
    public Boolean getData() { return this.data;}

    @JsonProperty
    public void setData(Boolean data) { this.data = data;}
}

But what I receieve from a GET request through browser or in my app via an ajax call:

{"ID":{"data":"123"},"Name":{"data":"MyName"},"InList":{},"IsParent":{}}
nullpointer
  • 490
  • 2
  • 4
  • 20
  • 1
    Did you test the backend in isolation from the frontend? What does `curl` request return? You can also try it with Postman. This will allow you to identify the problem (may be the frontend just handles it in wrong way, etc). The code of the annotated entity would also be helpful. – Sasha Shpota Sep 12 '17 at 22:40
  • As @OleksandrShpota mentioned, isolate client and server from each other. You can use `network` tab of the chrome developer tools or similar tools. Check your json response to determine the cause. – ykaragol Sep 13 '17 at 09:08
  • By the way we are using `Boolean` object, not primitive `boolean` in our Java objects. – ykaragol Sep 13 '17 at 09:08
  • @Oleksandr Shpota, I'll try with Postman and see where does it get me – nullpointer Sep 13 '17 at 13:01
  • @ykaragol, yes its `Boolean` not primitive. I looked at the returned `json` object in the front-end code and found out those variables were empty. – nullpointer Sep 13 '17 at 13:03
  • @OleksandrShpota @ ykaragol I tried a simple HTTP request, and the result was the same as in my web app. Meaning something wrong in Spring config perhaps? – nullpointer Sep 13 '17 at 18:22
  • @nullpointer as was already suggested provide more details. What is the entity you're trying to return as response? What is the actual response? What is "same result as in my web app"? What exactly? We can't help you knowing nothing. – Sasha Shpota Sep 13 '17 at 19:08
  • @OleksandrShpota I have updated how my class object is structured and what response I am getting. – nullpointer Sep 13 '17 at 19:37
  • The code seems valid. What version of spring are using? What json serializer are you using? This could happen in Jackson if you have configuration with `spring.jackson.default-property-inclusion=non_null` (means including only not null properties) and the property value is null. Do you have some specific serializer settings? – Sasha Shpota Sep 13 '17 at 21:03
  • @nullpointer Did you solve it? – ykaragol Sep 15 '17 at 09:40
  • 1
    @ykaragol I received `true` boolean values fine, so if they were empty I just assigned them false. – nullpointer Sep 15 '17 at 15:42

2 Answers2

0

You might be defining the getters/setters for your boolean values incorrectly.

With primitive types and other objects Spring uses the convention of get{VariableName}, but with booleans the format is different. An example is below:

boolean active;

public boolean isActive(){
  return active;
}

public void setActive(boolean active){
  this.active = active;
}

See related question here: For a boolean field, what is the naming convention for its getter/setter?

Rick Ridley
  • 573
  • 2
  • 9
  • Yes, the getters/ setters are defined like this. Also, my local system and local server both would have had trouble if they weren't defined properly. – nullpointer Sep 13 '17 at 12:59
0

In your get methods of InList and IsParent of class MyCustomObject, you don't created any object to return , only return this. but this. is initialized with null when an object of MyCustomObject is created. Look the correction :

@JsonProperty
public IsParent getIsParent() { return <object IsParent >}

... 

@JsonProperty
public InList getInList() { return <object InList >}

Do the same correction for setters.

Jorge SB
  • 155
  • 6