0

Please help to check this method. I got error http status 400 when I tried to post data using postman. Is there any mistake in this method?

@RequestMapping(value = "/Timestamp", method = RequestMethod.POST)
    @ResponseBody
    public Timestamp newTimestamp(@RequestBody Timestamp timestamp) {
        System.out.println("Test: "+ timestamp);
        timestampService.saveTimestamp(timestamp);
        return timestampDAO.findTimestampByPrimaryKey(timestamp.getId());
}

And This is my Timestamp class:

package ehealth.domain;

import java.io.Serializable;

import java.lang.StringBuilder;

import java.util.Calendar;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

import org.codehaus.jackson.annotate.JsonIgnore;

import javax.xml.bind.annotation.*;

import javax.persistence.*;

/**
 */

@Entity
@NamedQueries({
        @NamedQuery(name = "findAllTimestamps", query = "select myTimestamp from Timestamp myTimestamp"),
        @NamedQuery(name = "findTimestampByCreateDate", query = "select myTimestamp from Timestamp myTimestamp where myTimestamp.createDate = ?1"),
        @NamedQuery(name = "findTimestampById", query = "select myTimestamp from Timestamp myTimestamp where myTimestamp.id = ?1"),
        @NamedQuery(name = "findTimestampByIsActive", query = "select myTimestamp from Timestamp myTimestamp where myTimestamp.isActive = ?1"),
        @NamedQuery(name = "findTimestampByLoginDate", query = "select myTimestamp from Timestamp myTimestamp where myTimestamp.loginDate = ?1"),
        @NamedQuery(name = "findTimestampByLogoutDate", query = "select myTimestamp from Timestamp myTimestamp where myTimestamp.logoutDate = ?1"),
        @NamedQuery(name = "findTimestampByPrimaryKey", query = "select myTimestamp from Timestamp myTimestamp where myTimestamp.id = ?1"),
        @NamedQuery(name = "findTimestampByUpdateDate", query = "select myTimestamp from Timestamp myTimestamp where myTimestamp.updateDate = ?1") })

@Table(catalog = "ehealthdb", name = "timestamp")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "eHealth_Backend/ehealth/domain", name = "Timestamp")
@XmlRootElement(namespace = "eHealth_Backend/ehealth/domain")
public class Timestamp implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     */

    @Column(name = "id", nullable = false)
    @Basic(fetch = FetchType.EAGER)

    @Id
    @XmlElement
    Integer id;
    /**
     */
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "login_date")
    @Basic(fetch = FetchType.EAGER)

    @XmlElement
    Calendar loginDate;
    /**
     */
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "logout_date")
    @Basic(fetch = FetchType.EAGER)

    @XmlElement
    Calendar logoutDate;
    /**
     */
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "create_date", nullable = false)
    @Basic(fetch = FetchType.EAGER)

    @XmlElement
    Calendar createDate;
    /**
     */
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "update_date")
    @Basic(fetch = FetchType.EAGER)

    @XmlElement
    Calendar updateDate;
    /**
     */

    @Column(name = "isActive")
    @Basic(fetch = FetchType.EAGER)

    @XmlElement
    Boolean isActive;

    /**
     */
    @OneToMany(mappedBy = "timestamp", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)

    @XmlElement(name = "", namespace = "")
    java.util.Set<ehealth.domain.User> users;

    /**
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     */
    public Integer getId() {
        return this.id;
    }

    /**
     */
    public void setLoginDate(Calendar loginDate) {
        this.loginDate = loginDate;
    }

    /**
     */
    public Calendar getLoginDate() {
        return this.loginDate;
    }

    /**
     */
    public void setLogoutDate(Calendar logoutDate) {
        this.logoutDate = logoutDate;
    }

    /**
     */
    public Calendar getLogoutDate() {
        return this.logoutDate;
    }

    /**
     */
    public void setCreateDate(Calendar createDate) {
        this.createDate = createDate;
    }

    /**
     */
    public Calendar getCreateDate() {
        return this.createDate;
    }

    /**
     */
    public void setUpdateDate(Calendar updateDate) {
        this.updateDate = updateDate;
    }

    /**
     */
    public Calendar getUpdateDate() {
        return this.updateDate;
    }

    /**
     */
    public void setIsActive(Boolean isActive) {
        this.isActive = isActive;
    }

    /**
     */
    public Boolean getIsActive() {
        return this.isActive;
    }

    /**
     */
    public void setUsers(Set<User> users) {
        this.users = users;
    }

    /**
     */
    @JsonIgnore
    public Set<User> getUsers() {
        if (users == null) {
            users = new java.util.LinkedHashSet<ehealth.domain.User>();
        }
        return users;
    }

    /**
     */
    public Timestamp() {
    }

    /**
     * Copies the contents of the specified bean into this bean.
     *
     */
    public void copy(Timestamp that) {
        setId(that.getId());
        setLoginDate(that.getLoginDate());
        setLogoutDate(that.getLogoutDate());
        setCreateDate(that.getCreateDate());
        setUpdateDate(that.getUpdateDate());
        setIsActive(that.getIsActive());
        setUsers(new java.util.LinkedHashSet<ehealth.domain.User>(that.getUsers()));
    }

    /**
     * Returns a textual representation of a bean.
     *
     */
    public String toString() {

        StringBuilder buffer = new StringBuilder();

        buffer.append("id=[").append(id).append("] ");
        buffer.append("loginDate=[").append(loginDate).append("] ");
        buffer.append("logoutDate=[").append(logoutDate).append("] ");
        buffer.append("createDate=[").append(createDate).append("] ");
        buffer.append("updateDate=[").append(updateDate).append("] ");
        buffer.append("isActive=[").append(isActive).append("] ");

        return buffer.toString();
    }

    /**
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
        return result;
    }

    /**
     */
    public boolean equals(Object obj) {
        if (obj == this)
            return true;
        if (!(obj instanceof Timestamp))
            return false;
        Timestamp equalCheck = (Timestamp) obj;
        if ((id == null && equalCheck.id != null) || (id != null && equalCheck.id == null))
            return false;
        if (id != null && !id.equals(equalCheck.id))
            return false;
        return true;
    }
}

Json data using Postman

 {
 "id":4,
 "loginDate":"01/30/2016 12:12:12 AM",
 "logoutDate":"01/30/2016 12:12:12 AM",
 "createDate":"01/30/2016 12:12:12 AM",
 "updateDate":"01/30/2016 12:12:12 AM",
 "isActive":true
}
Lay Leangsros
  • 9,156
  • 7
  • 34
  • 39
  • how does the JSON look you send with postman,, can you post it? – kamokaze Mar 23 '16 at 20:25
  • @kamokaze Yes, updated. Please help. Thanks – Lay Leangsros Mar 23 '16 at 20:32
  • 1
    The data you send has to map to the properties. For example you have an argument called timestamp and nothing in your JSON represents this. Also if you are posting either you need to create an object that represents all of the values or else determine which value you want. see http://stackoverflow.com/questions/11291933/requestbody-and-responsebody-spring. – ismoore999 Mar 23 '16 at 22:18
  • Sorry thought your Timestamp class was the standard Timestamp. Can you post that class too – ismoore999 Mar 23 '16 at 22:21
  • @ismoore999 Thank you, and I updated it. Please check it. – Lay Leangsros Mar 24 '16 at 10:59
  • ok, well the pointer I can give you is to look up is that the date formats I believe by default are expected to be ISO 8601 (looks like you are using codehaus for your json parsing). I suggest you look up how to define how a date is to be parsed. – ismoore999 Mar 24 '16 at 16:08
  • @ismoore999 thank you so much. Yeah, you're right. :D – Lay Leangsros Mar 24 '16 at 19:16

1 Answers1

1

look up is that the date formats I believe by default are expected to be ISO 8601 (looks like you are using codehaus for your json parsing). I suggest you look up how to define how a date is to be parsed or change your date format in the JSON if that is possible

ismoore999
  • 163
  • 11