I'm developing a CRUD application with a REST interface in Java-EE.
I have some entities which contains Date fields. When I want to create an instance of such an entity with a POST request with the JSON in the request body, jax-rs (or the underlying deserializer) complains about parsing the date part.
This is the exception I got:
Servlet.service() for servlet service.JAXRSConfiguration threw exception java.time.format.DateTimeParseException: Text '2011-11-11' could not be parsed at index 10
I tried to send this in the request:
{
"title": "testTitle",
"description": "testDescription",
"playtime": 50,
"creationDate": "2011-11-11"
}
How should I define the date in the json to get it parsed successfully? Which is the correct format?
Here is the entity class:
@Entity
@NamedQueries({
@NamedQuery(name = "Movie.findAll", query = "SELECT m FROM Movie m")
})
public class Movie extends AbstractDao{
private String title;
private String description;
@ManyToMany(mappedBy = "movies")
private List<Actor> actors;
@ManyToMany(mappedBy = "movies")
private List<Director> directors;
private int playtime;
@OneToOne(cascade = CascadeType.PERSIST)
private Trailer trailer;
@Temporal(value = TemporalType.DATE)
private Date creationDate;
getters,setters, etc
Here is the jax-rs service:
@Path("/movies")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class MovieRestService {
@Inject
private MovieService movieService;
@GET
public List<Movie> getMovies(){
return movieService.findAll();
}
@Path("{id}")
@GET
public Movie getMovie(@PathParam("id") long id){
return movieService.findById(id);
}
@POST
public Movie addMovie(Movie movie){
return movieService.create(movie);
}
@Path("{id}")
@PUT
public Movie updateMovie(Movie movie, @PathParam("id") long id){
return movieService.update(movie,id);
}
@Path("{id}")
@DELETE
public Movie deleteMovie(@PathParam("id") long id){
return movieService.delete(id);
}
}