0

I'm trying to post data from my client to a rest-webserver. My client uses Spring's Resttemplate for Android.

Requesting objects works, but POSTing them runs in the following error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.erdi.mobilefinalneu/com.example.erdi.mobilefinalneu.UploadActivity}: java.lang.IllegalStateException: Failed to instantiate standard serializer (of type com.fasterxml.jackson.databind.ser.std.NullSerializer): access to constructor not allowed

Caused by: java.lang.IllegalStateException: Failed to instantiate standard serializer (of type com.fasterxml.jackson.databind.ser.std.NullSerializer): access to constructor not allowed

The relevant code is:

    restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    //restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
    StrictMode.ThreadPolicy policy = new
            StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    HttpEntity<RecordingModel> entity = new HttpEntity<RecordingModel>(recordingToPost,headers);

    restTemplate.put(url + "sportsupload/", entity);
    //ResponseEntity<RecordingModel> out = restTemplate.exchange(url + "sportsupload/", HttpMethod.POST, entity
    //        , RecordingModel.class);
    //restTemplate.postForLocation(url + "sportsupload/", recordingToPost);

(as you can see in the commented lines, I tried different ways to post an object, but all had these error message)

My RecordingModel-object looks like the following:

@JsonIgnoreProperties(ignoreUnknown = true)
//@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class RecordingModel {
private String upload_date;
private String recording_date;
private String name;
private String user;
private String video;
private List<String> sensors = new ArrayList<String>();

public String getName(){
    return name;
}

public List<String> getSensors() {
    return sensors;
}

public String getRecording_date() {
    return recording_date;
}

public String getUpload_date() {
    return upload_date;
}

public String getUser() {
    return user;
}

public String getVideo() {
    return video;
}

public void getRecordingDetails(){
    if(upload_date != null){
        Log.d("RecordingModel", upload_date);
    }
    if(recording_date != null){
        Log.d("RecordingModel", recording_date);
    }
    if(name != null){
        Log.d("RecordingModel", name);
    }
    if(user != null){
        Log.d("RecordingModel", user);
    }
    if(video != null){
        Log.d("RecordingModel", video);
    }
    for(String sensor : sensors){
        Log.d("RecordingModel", sensor);
    }
}

public void setName(String name) {
    this.name = name;
}

public void setRecording_date(String recording_date) {
    this.recording_date = recording_date;
}

public void setSensors(List<String> sensors) {
    this.sensors = sensors;
}

public void setUpload_date(String upload_date) {
    this.upload_date = upload_date;
}

public void setUser(String user) {
    this.user = user;
}

public void setVideo(String video) {
    this.video = video;
}

}

As you can see, I tried to use JSONSerialize there, but that also did not work.

Has anyone of you an idea what I am doing wrong? Googling/Stackoverflowing arround did not help me...

Stefan Wegener
  • 726
  • 1
  • 10
  • 25

1 Answers1

0

instead of @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)

try

@JsonInclude(JsonInclude.Include.NON_NULL)

depending on Jacson version you use, it may be different.

Community
  • 1
  • 1
ashoke
  • 6,441
  • 2
  • 26
  • 25