0

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);
    }

}
Vendel Serke
  • 135
  • 11
  • Did you look at https://stackoverflow.com/questions/12463049/date-format-mapping-to-json-jackson? – clinomaniac Apr 03 '18 at 17:41
  • Actually I think i have come across that question, but its a different scenario imo. – Vendel Serke Apr 03 '18 at 17:44
  • Can you try to read the data as json and see what format you get? Create some data on the Java side and try to read it as json. – clinomaniac Apr 03 '18 at 17:46
  • You could try to add annotation to date field as in this question https://stackoverflow.com/questions/13803391/serialize-date-in-a-json-rest-web-service-as-iso-8601-string and one mentioned by @clinomaniac – Ivan Apr 03 '18 at 17:56

1 Answers1

0

Try to use a DateAdapter (XmlAdapter) like the following one:

And annotate:

@XmlJavaTypeAdapter(DateAdapter.class)
private Date creationDate;

You can also annotate at the getter of creationDate

Class:

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class DateAdapter extends XmlAdapter<String, Date> {
  private static final TimeZone UTC = TimeZone.getTimeZone("UTC");    
  private static final String PATTERN_T_WITH_SEC = "yyyy-MM-dd'T'HH:mm:ss";
  private SimpleDateFormat dateFormat = new impleDateFormat(PATTERN_T_WITH_SEC);

@Override
public String marshal(Date v) throws Exception {
    dateFormat.setTimeZone(UTC);
    String dateF = dateFormat.format(v);
    return dateF;
}

@Override
public Date unmarshal(String v) throws Exception {
    dateFormat.setTimeZone(UTC);
    Date date = dateFormat.parse(v);
    return date;
}

}