Matheus Bica
Matheus Bica

Reputation: 1095

How to receive and send Date in Spring Rest using JSON?

I'am working in a project right now and I have been trying to receive and send a Date in a Json form and store that on my Model/DataBase.

My Model is something like this:

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

I would like to receive a List of Incidents, that was working when all this Date were String(just for tests).

But now I wish to pass them all to Date. My current format changes from each Date column, for example, uploadDate is received as "dd.MM.yyyy HH:mm:ss" and lastDate is received as "dd.MM.yyyy HH:mm"(without seconds).

My POST at Controller is:

@RequestMapping(method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public @ResponseBody List<Incident> create(@RequestBody List<Incident> incident) {
         for(.....//runs all the list and insert one by one)
         {
              IncidentDao.createIncident(incident_item);
         }
         return incident;
}

And at DAO:

@Transactional
    public Incident createIncident(Incident incident){
        em.persist(incident);
        return incident;
    }

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.

It were all working before changing type to date. 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.

My GET is like this at DAO:

public Incident getIncidentById(String id){
        Incident incident = em.find(Incident.class, id);
        return incident;
    }

At Controller:

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public @ResponseBody Incident findById(@PathVariable String id) {
        return taskDao.getIncidentById(id);
    }

My GET that returns the entire list of incidents at DAO:

public Collection<Incident> getAllIncidents(){
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Incident> criteria = cb.createQuery(Incident.class);
        Collection<Incident> resultList = em.createQuery(criteria).getResultList();
        return resultList;
    }

At Controller:

@RequestMapping(method = RequestMethod.GET)
    public @ResponseBody Collection<Incident> findAll() {
        return IncidentDAO.getAllIncidents();
    }

Also I would like to know if one of my Dates are Id also, it will change something on the solution(the POST part and GET part - besides the Class ID I should create when the model has multiple Id's)?

Thanks for any help, have a great day!

Upvotes: 1

Views: 6034

Answers (2)

Matheus Bica
Matheus Bica

Reputation: 1095

Found a solution, the problem is the date at Spring is received as "YYYY-MM-ddTHH:mm:ss" so if you want to send it directly into your database you must send in this format or convert it to your database format in the moment you will persist the data.

I do not found an working way to change the format that Spring receives. I just change the format when someone does a GET to receive the information in more friendly format.

For people that like me need date's as ID's for some random reason. Thanks!

Upvotes: 0

Nguyen Tuan Anh
Nguyen Tuan Anh

Reputation: 1036

uploadDate is received as "dd.MM.yyyy HH:mm:ss" and lastDate is received as "dd.MM.yyyy HH:mm"(without seconds).

This actually doesn't really make sense. If you would like to display your dates the way you want, then format the date in your view. In your database or when you are processing your dates, just leave the format as it is.

if one of my Dates are Id also, it will change something on the solution

Well, this may not be a good idea either. Sorry. You have tons of options for ID generation, why bother using date?

Upvotes: 1

Related Questions