Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android REST POST loses Date value

I have an Android App (Spring Android + Android Annotations + Gson) that consume REST services from a Web App (Jersey + Spring + Hibernate / JPA). The problem is that my java.util.Date properties are not serialized:

Activity (Android App):

...
@Click(R.id.buttonLogin)
void onLoginClick() {
    Student student = new Student();
    student.setDateRegistration(new Date()); //Property where the problem occurs
    student.setFirstName("Venilton");
    student.setGender(Gender.M);

    doSomethingInBackground(student);
}

@Background
void doSomethingInBackground(Student student) {
    this.restClient.insert(student);
}
...

Rest Client (Android App):

@Rest(rootUrl = "http://MY_IP:8080/restful-app", 
    converters = { GsonHttpMessageConverter.class })
public interface StudentRESTfulClient {

    @Post("/student/insert")
    Student insert(Student student);
}

Rest Server (Web App):

@Component
@Path("/student")
public class StudentRESTfulServer {

    @Autowired
    private StudentService studentServiceJpa;

    @POST 
    @Path("/insert")
    public Student insert(Student student) {
        //student.getDateRegistration() is null! It's a problem!

        Student studentResponse = null;
        try {
                this.studentServiceJpa.insert(student);
                studentResponse = student;
        } catch (Exception exception) { }

        return studentResponse;
    }
}

The Android application performs the POST the Student object for REST service, but the DateRegistration property loses its value when the student object arrives in StudentRESTfulServer.

Could you help me?

like image 204
falvojr Avatar asked Oct 21 '22 19:10

falvojr


1 Answers

Apparently Gson doesn't know how to properly serialize your date (a bit weird that it doesn't throw anything into the log, or does it?)

The simple solution would be setting Gson date format you are going to use. For this you need to create custom converter and use it instead of GsonHttpMessageConverter

CustomHttpMessageConverter.java

CustomHttpMessageConverter extends GsonHttpMessageConverter {
    protected static final String DATE_FORMAT = "yyyy-MM-dd";

    protected static Gson buildGson() {
        GsonBuilder gsonBuilder = new GsonBuilder();

        gsonBuilder.setDateFormat(DATE_FORMAT);

        return gsonBuilder.create();
    }

    public CustomHttpMessageConverter()  {
        super(buildGson());
    }
}

Then in your REST Client (Android app)

@Rest(rootUrl = "http://MY_IP:8080/restful-app", 
converters = { CustomHttpMessageConverter.class })

And that should work just fine.

If still not working

Then you can add whatever settings you need for Gson inside buildGson method, e.g. you can register custom serializers if you need some:

    gsonBuilder.registerTypeAdapter(Date.class, new GsonDateDeSerializer());

but then you'd need to implement JsonDeserializer and JsonSerializer interfaces within your GsonDateDeSerializer class.

For custom serializing/deserializing you can check out my other answer: GSON Serialize boolean to 0 or 1

like image 63
Damian Walczak Avatar answered Oct 23 '22 21:10

Damian Walczak