title: How to receive and send Date in Spring Rest using JSON?
body: <p>I'am working in a project right now and I have been trying <strong>to receive and send a Date in a Json form and store that on my Model/DataBase</strong>.</p>
  
  <p>My <strong>Model</strong> is something like this:</p>
  
  <pre><code>@Entity
  public class Incident  {
      @Id
      private String id; //it contains sometimes letters, thats why its String
  
      @Column(nullable = false)
      private String responsible;
  
      @Temporal(TemporalType.TIMESTAMP)
      @Column(nullable = false)
      private Date uploadDate;
  
      @Temporal(TemporalType.TIMESTAMP)
      @Column(nullable = false)
      private Date lastDate;
  }
  </code></pre>
  
  <p>I would like to receive a List of Incidents, that was working when all this Date were String(just for tests).</p>
  
  <p>But now I wish to pass them all to Date.
  <strong>My current format changes from each Date column</strong>, for example, <strong>uploadDate is received as "dd.MM.yyyy HH:mm:ss" and lastDate is received as "dd.MM.yyyy HH:mm"(without seconds).</strong></p>
  
  <p>My <strong>POST at Controller</strong> is:</p>
  
  <pre><code>@RequestMapping(method = RequestMethod.POST)
      @ResponseStatus(HttpStatus.CREATED)
      public @ResponseBody List&lt;Incident&gt; create(@RequestBody List&lt;Incident&gt; incident) {
           for(.....//runs all the list and insert one by one)
           {
                IncidentDao.createIncident(incident_item);
           }
           return incident;
  }
  </code></pre>
  
  <p>And at <strong>DAO</strong>:</p>
  
  <pre><code>@Transactional
      public Incident createIncident(Incident incident){
          em.persist(incident);
          return incident;
      }
  </code></pre>
  
  <p>What should I do to my json that sends that the way I said before, accepts some format, some people serialize it, but I would like to receive it in the format it is.</p>
  
  <p>It were all working before changing type to date.
  <strong><em>I imagine I should use the @DateTimeFormat with pattern but I really don't know where to put this, if it is in the model or in the controller/DAO or if it is in everything.</em></strong> </p>
  
  <p>My <strong>GET</strong> is like this at <strong>DAO</strong>:</p>
  
  <pre><code>public Incident getIncidentById(String id){
          Incident incident = em.find(Incident.class, id);
          return incident;
      }
  </code></pre>
  
  <p>At <strong>Controller</strong>:</p>
  
  <pre><code>@RequestMapping(value = "/{id}", method = RequestMethod.GET)
      public @ResponseBody Incident findById(@PathVariable String id) {
          return taskDao.getIncidentById(id);
      }
  </code></pre>
  
  <p>My <strong>GET</strong> that returns the e<strong>ntire list of incidents at DAO:</strong></p>
  
  <pre><code>public Collection&lt;Incident&gt; getAllIncidents(){
          CriteriaBuilder cb = em.getCriteriaBuilder();
          CriteriaQuery&lt;Incident&gt; criteria = cb.createQuery(Incident.class);
          Collection&lt;Incident&gt; resultList = em.createQuery(criteria).getResultList();
          return resultList;
      }
  </code></pre>
  
  <p>At <strong>Controller</strong>:</p>
  
  <pre><code>@RequestMapping(method = RequestMethod.GET)
      public @ResponseBody Collection&lt;Incident&gt; findAll() {
          return IncidentDAO.getAllIncidents();
      }
  </code></pre>
  
  <p>Also I would like to know <strong>if one of my Dates are Id also, it will change something on the solution</strong>(the POST part and GET part - besides the Class ID I should create when the model has multiple Id's)<strong>?</strong></p>
  
  <p>Thanks for any help, have a great day!</p>