Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey JSON and Date

I have a problem with parsing JSON to Date object.

I use Jersey 1.14, Tomcat 7.

My Resource class:

    @Path("/user")
    @Produces(MediaType.APPLICATION_JSON)
    public class UserResource {
        @Path("/~/update")
        @POST
        @Consumes(MediaType.APPLICATION_JSON)
        public Response updateUser(User user) {
            // There are update in DB
            // but Date field always null
            return Response.ok().build();
        }
        @Path("/~/get")
        @GET
        public User getUser() {
            // There are I fetch User from DB
            // Works fine, User instance returns in correct JSON
            return user;
        }

    }

My Model:

    @XmlAccessorType(XmlAccessType.NONE)
    @XmlRootElement(name = "user")
    public class User {
        @XmlElement(name = "name")
        private String name;
        @XmlElement(name = "myDate")
        @XmlJavaTypeAdapter(DateAdapter.class)
        private Date myDate;
        // Getters and Setters
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Date getMyDate() {
            return myDate;
        }
        public void setMyDate(Date myDate) {
            this.myDate = myDate;
        }
    }

My XmlAdapter for Date class:

    public class DateAdapter extends XmlAdapter<String, Date> {

        private static final SimpleDateFormat dateFormat = new SimpleDateFormat(
        "yyyy-MM-dd");

        @Override
        public String marshal(Date v) {
            return dateFormat.format(v);
        }

        @Override
        public Date unmarshal(String v) {
            try {
                return dateFormat.parse(v);
            } catch (ParseException e) {
                throw new WebApplicationException();
            }
        }
    }

Why in my POST method always null Date field? This JSON I am trying to send:

{"name":"Peter","myDate":"1988-05-31"}

P.S. sorry for my bad English.

UPD. My client code:

    public class Tester {

        public static void main(String... args) throws FileNotFoundException, JSONException {

            Client c = Client.create();
            WebResource r = c.resource("http://localhost:8080/myapp/user/~/get");
            ClientResponse resp = r.get(ClientResponse.class);
            JSONObject entity = resp.getEntity(JSONObject.class);
            entity.remove("myDate");
            entity.put("myDate", "1988-05-31");
            r = c.resource("http://localhost:8080/myapp/user/~/update");
            resp = r.entity(entity, MediaType.APPLICATION_JSON).post(ClientResponse.class);
            entity = resp.getEntity(JSONObject.class);
            System.out.println(entity);
        }
    }     

UPD 2.

I found a very stupid mistake in my app. Instead java.util.Date I used java.sql.Date in my Model class :(
Thats why Date field always was null.

like image 373
chepiov Avatar asked Nov 28 '12 18:11

chepiov


1 Answers

Your server code are right. The error is in your client code:

resp = r.entity(entity, MediaType.APPLICATION_JSON).post(ClientResponse.class);

should be:

resp = r.type(MediaType.APPLICATION_JSON).post(ClientResponse.class,entity);
like image 138
secondflying Avatar answered Nov 01 '22 01:11

secondflying