Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android REST POST丢失日期值_Java_Android_Rest_Gson_Android Annotations - Fatal编程技术网

Java Android REST POST丢失日期值

Java Android REST POST丢失日期值,java,android,rest,gson,android-annotations,Java,Android,Rest,Gson,Android Annotations,我有一个Android应用程序(Spring Android+Android Annotations+Gson),它使用Web应用程序(Jersey+Spring+Hibernate/JPA)提供的REST服务。问题是我的java.util.Date属性没有序列化: 活动(Android应用程序): ... @Click(R.id.buttonLogin) void onLoginClick() { Student student = new Student(); student

我有一个Android应用程序(Spring Android+Android Annotations+Gson),它使用Web应用程序(Jersey+Spring+Hibernate/JPA)提供的REST服务。问题是我的java.util.Date属性没有序列化:

活动(Android应用程序):

...
@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(rootUrl = "http://MY_IP:8080/restful-app", 
    converters = { GsonHttpMessageConverter.class })
public interface StudentRESTfulClient {

    @Post("/student/insert")
    Student insert(Student student);
}
@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;
    }
}
Rest客户端(Android应用程序):

...
@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(rootUrl = "http://MY_IP:8080/restful-app", 
    converters = { GsonHttpMessageConverter.class })
public interface StudentRESTfulClient {

    @Post("/student/insert")
    Student insert(Student student);
}
@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;
    }
}
Rest服务器(Web应用):

...
@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(rootUrl = "http://MY_IP:8080/restful-app", 
    converters = { GsonHttpMessageConverter.class })
public interface StudentRESTfulClient {

    @Post("/student/insert")
    Student insert(Student student);
}
@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;
    }
}
Android应用程序执行REST服务的学生对象发布,但当学生对象到达StudentRESTfulServer时,DateRegistration属性将失去其值。


你能帮我吗?

显然,Gson不知道如何正确地序列化你的日期(有点奇怪,它没有向日志中添加任何内容,是吗?)

简单的解决方案是设置您将要使用的Gson日期格式。 为此,您需要创建自定义转换器并使用它,而不是
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());
    }
}
然后在您的REST客户端(Android应用程序)

这应该很管用

如果仍然不工作

然后,您可以在
buildGson
方法中添加Gson所需的任何设置,例如,如果需要,您可以注册自定义序列化程序:

    gsonBuilder.registerTypeAdapter(Date.class, new GsonDateDeSerializer());
但是,您需要在
GsonDateDeSerializer
类中实现
JsonDeserializer
JsonSerializer
接口


对于自定义序列化/反序列化,您可以查看我的另一个答案:

请从客户端和服务器发布您的学生模型。我强烈感觉Date()不能在json上序列化,所以您应该使用字符串或长值。您好@MemLeak,我的学生实体很好:
@XmlRootElement
公共类学生实现可序列化{
@Column(name=“Date\u last\u login”,nullable=false)
私人日期dateLastLogin;
尽量将时间戳存储为Date:student.setDateRegistration(System.currentTimeMillis())Damian Walczak感谢您的回答!对我来说非常有意义,我们将尽快进行测试。