Vivek Shankar S
Vivek Shankar S

Reputation: 3

JSONObject to Java bean class not working

I have written a webservice in jersy client and converting a json string to bean class but date is showing error . I tried @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")but still it gives the same error in the method. How to convert the String to date format in my code.

Error:

not a valid representation (error: Failed to parse Date value 'Aug 7, 2017 1:35:00 PM': Can not parse date "Aug 7, 2017 1:35:00 PM": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))

Webservice

    @POST
    @Path("/driverLocation")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response handleDriverLocation(String jsonRequest) {
        List<DriverLocationDetails> driverLocationDetails = bookingService
                .getLocationDetailsFromDB();
        boolean save = false;
        for (DriverLocationDetails details : driverLocationDetails) {
            Gson g = new Gson();
            jsonRequest = g.toJson(details);
            DriverLocationDetails driverLocationDetail = TmsUtil
                    .readAsObjectOf(DriverLocationDetails.class, jsonRequest);
            save = bookingService.saveLocation(driverLocationDetail);
        }

        JSONObject jo = new JSONObject();
        try {
            if (save) {
                jo.put("Save", "saved");
            } else {
                jo.put("Save", "failed");
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return Response.status(200).entity(jo.toString()).build();

    }

static com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper();
public static <T> T readAsObjectOf(Class<T> clazz, String value) {
        try {
            return MAPPER.readValue(value, clazz);
        } catch (Exception e) {
            logger.error("{}, {}", e.getMessage(), e.fillInStackTrace());
        }
        return null;
    }

Console

Can not construct instance of java.util.Date from String value 'Aug 7, 2017 1:35:00 PM': not a valid representation (error: Failed to parse Date value 'Aug 7, 2017 1:35:00 PM': Can not parse date "Aug 7, 2017 1:35:00 PM": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
 at [Source: java.io.StringReader@4d768e3c; line: 1, column: 72] (through reference chain: in.greenorange.model.DriverLocationDetails["gpsLocationTime"]), com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value 'Aug 7, 2017 1:35:00 PM': not a valid representation (error: Failed to parse Date value 'Aug 7, 2017 1:35:00 PM': Can not parse date "Aug 7, 2017 1:35:00 PM": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
 at [Source: java.io.StringReader@4d768e3c; line: 1, column: 72] (through reference chain: in.greenorange.model.DriverLocationDetails["gpsLocationTime"])

Upvotes: 0

Views: 156

Answers (2)

Vivek Shankar S
Vivek Shankar S

Reputation: 3

I simply used GSon to convert as jackson has conversions

DriverLocationDetails driverLocationDetail = g.fromJson(jsonRequest, DriverLocationDetails.class);

Upvotes: 0

Jens
Jens

Reputation: 69450

Your pattern must look like:

@JsonFormat(pattern = "MMM d',' yyyy HH:mm:ss a")

Because this is the pattern to you get from the server

Upvotes: 1

Related Questions