Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting JsonMappingException while sending data to view

Tags:

I am trying to show DB data to my webpage. I have made following code when GET request to the @RequestMapping(value = "/api/binder").

but when get request came to this method it will fetch data (I have print on console and display well) but it doesn't map to my Java Script Ajax call, it's showing me an error.

Following is my code for to fetch data :

    @Autowired
    IBinderViewRepository repository;

    @RequestMapping(method= RequestMethod.GET)
    public @ResponseBody
    List<BinderResponse> getBinders(){
        List<BinderView> binders = repository.getBinders();
        List<BinderResponse> responses = new ArrayList<>();
        ModelMapper mapper = Mapper.getInstance();

        for(int i = 0; i < binders.size(); i++){
            System.out.println("In Loop");
            BinderResponse response = mapper.map(binders.get(i),BinderResponse.class);
            System.out.println("Data :: " + response.getBinderName());
            responses.add(response);
        }
        return responses;
    }

but it shows me following error :

HTTP Status 500 - Could not write JSON: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->com.ngl.dto.outgoing.BinderResponse["valid"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->com.ngl.dto.outgoing.BinderResponse["valid"])

Here is ajax call from knockout js :

ajax.get('api/binder').done(function(response){ ... }

Here BinderView and BinderResponse have same fields :

    private String binderName;
    private String binderAddress1;

and getter setter as well in both. and repository.genBinders() method bring data from DB.

Here is insert method and works fine for me :

    @RequestMapping(method= RequestMethod.POST,consumes = "application/json")
    public @ResponseBody
    IWebApiResponse addBinder(@RequestBody AddBinderForm binder){
        .....
    }

Shall I have to put any json annotation on my BinderResponse class ?

I don't understand where am i wrong ?Anyone pleas guide me.

UPDATE :

public class BinderResponse extends WebApiResponseBase {
    private String binderName;
    private String binderAddress1;

public String getBinderName() {
        return binderName;
    }

    public void setBinderName(String binderName) {
        this.binderName = binderName;
    }

    public String getBinderAddress1() {
        return binderAddress1;
    }

    public void setBinderAddress1(String binderAddress1) {
        this.binderAddress1 = binderAddress1;
    }
}

BinderView :

    public class BinderView extends BaseView {
        private String binderName;
        private String binderAddress1;
    public String getBinderName() {
            return binderName;
        }

        public void setBinderName(String binderName) {
            this.binderName = binderName;
        }

        public String getBinderAddress1() {
            return binderAddress1;
        }

        public void setBinderAddress1(String binderAddress1) {
            this.binderAddress1 = binderAddress1;
        }

}

In console it prints data / BinderName :

In Loop
Data :: ada
In Loop
Data :: tya

New Update :

Here is BaseView :

@MappedSuperclass
public abstract class BaseView implements IEntity {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    @Column(name="id")
    private long id;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        if (this.id != 0 && this.id != id) {
            throw new IllegalStateException(
                    "The ID must not be changed after it is set.");
        }
        this.id = id;
    }
}

and In IEntity :

public interface IEntity  extends Serializable {
    long getId();
    void setId(long id);
}

WebApiResponseBase :

public class WebApiResponseBase implements IWebApiResponse {

    private String _uri;

    @Override
    public String getUri() {
        return _uri == null ? "" : _uri;
    }

    @Override
    public void setUri(String uri) {
        _uri = uri;
    }
}
like image 711
user3145373 ツ Avatar asked Apr 04 '14 04:04

user3145373 ツ


2 Answers

Jackson, by default, serializes an object's whole inheritance hierarchy, ie. the parent class fields as well. In the case of

public class BinderResponse extends WebApiResponseBase {

it seems like

Could not write JSON: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->com.ngl.dto.outgoing.BinderResponse["valid"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->com.ngl.dto.outgoing.BinderResponse["valid"])

Jackson tries to serialize a field called valid from a getter called isValid (which is a conventional bean property name). The getter method, however, seems to throw a NullPointerException for whatever reason.

If you want Jackson to ignore it, you can annotate the getter with @JsonIgnore or your class with @JsonIgnoreProperties and specify the property name, ie. valid.

like image 68
Sotirios Delimanolis Avatar answered Oct 25 '22 06:10

Sotirios Delimanolis


In my case when I used @JsonIgnore the exception has been gone but the problem was it couldn't receive that value from API Request anymore and Spring ignored it (obviously because of @JsonIgnore) So I investigated about the issue and figured out that the problem was the getter and setter. I had the Integer property while my getter was int. So when I changed the getter to Integer my problem solved and error's gone.

private Integer purchaseId;

@JsonIgnore
public int getPurchaseId() {
    return purchaseId;
}

public void setPurchaseId(int purchaseId) {
    this.purchaseId = purchaseId;
}

Changed to :

private Integer purchaseId;


public Integer getPurchaseId() {
    return purchaseId;
}

public void setPurchaseId(Integer purchaseId) {
    this.purchaseId = purchaseId;
}
like image 41
Far Zad Avatar answered Oct 25 '22 08:10

Far Zad