0

I've seen similar questions answered on here, but none of the solutions are working for me. I have a javax.ws.rs REST API, and I'm trying to parse a java.util.Date, but can't seem to manage it. I believe the date parser is Jackson. How can I go about doing this? The error string talks about java.util.Calendar, but I'm not using that object, does that have something to do with it?

@POST
@Consumes(APPLICATION_JSON)
@Path("/tester")
@ApiOperation(value = "", response = String.class)
public String test(Tester tester) {
    System.out.println(tester.getStr().toString());
    throw new WebApplicationException(Response.Status.NOT_IMPLEMENTED);
}

My POJO class:

public class Tester {
    @JsonSerialize(as=Date.class)
    @JsonFormat(shape= JsonFormat.Shape.STRING, pattern="MM/dd/yyyy")
    private Date date;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}

I get a 400 Bad Request with the output:

Exception Description: The object [12/11/1999],
  of class [class java.lang.String],
  from mapping [org.eclipse.persistence.oxm.mappings.XMLDirectMapping[str-->str/text()]] with descriptor [XMLDescriptor(canner.model.Tester --> [])],
  could not be converted to [class java.util.Calendar]."
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Hackerman
  • 1,289
  • 1
  • 14
  • 29
  • Did you try this https://stackoverflow.com/questions/8745305/how-to-handle-java-util-date-with-moxy-bindings-file – Eklavya Jun 13 '20 at 19:05
  • 2
    Consider not using `java.util.Date`. That class is poorly designed and long outdated. Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jun 13 '20 at 19:26
  • 1
    @OleV.V. I've switched over to LocalDate, but @JsonFormat(pattern = "MM/dd/yyyy") above the variable still does nothing – Hackerman Jun 13 '20 at 20:35
  • It seems like your JAX-RS implementation parses request data as XML format with EclipseLink. Please check your project jar dependencies and configurations, make sure Jackson is used during parsing. Also, HTTP header `Content-Type: application/json` should be provided in your request to make sure JAX-RS implementation clearly knows which format should be used to parsing data. – HowieChih Jun 14 '20 at 03:41
  • You and the first commenter are right, but I can’t exclude the dependencies (for other reasons) to use Jackson. Thanks for your help – Hackerman Jun 15 '20 at 05:03

0 Answers0