1

I have used ObjectMapper to map a object to Json String. I have date Fields in the object to format the date i have used the below code but it is not formatting as expected.

My Json String:

{"leaveRequestId":51,"reason":"xdvfsgf","leaveFromDate":"2016-07-13","leaveToDate":"2016-07-15","leaveTypeId":9,"statusId":1,"instanceId":"7527","createdBy":"pramod","createdOn":"2016-07-07","modifiedBy":null,"modifiedOn":null}

I am using the below code:

@RequestMapping(value="/getLeaveRequest", method = RequestMethod.GET)
@ResponseBody
public  String getLeaveRequest( int leaveRequestId) throws Exception {
 DAOFactory obj_daofactory=new DAOFactory();
 ObjectMapper mapper = new ObjectMapper();
 SimpleDateFormat df = new SimpleDateFormat("yyyy-MMM-dd");
 mapper.setDateFormat(df);

 LeaveRequest leaveRequest = obj_daofactory.getLeaveRequestDao().findByLeaveRequestId(leaveRequestId);
 if(leaveRequest.getLeaveRequestId() == 0){
     return "No data found";
 } else {
     System.out.println(leaveRequest.getLeaveFromDate().toString());
     String jsonInString = mapper.writeValueAsString(leaveRequest);
     System.out.println(jsonInString);
     return jsonInString;
    }
}

MY Expected OutPut:

{"leaveRequestId":45,"reason":"test","leaveFromDate":"2016-Jul-07","leaveToDate":"2016-Jul-08","leaveTypeId":9,"statusId":1,"instanceId":"test1","createdBy":"deepak.paul@muraai.com","createdOn":"2016-Jul-07","modifiedBy":"pramod","modifiedOn":"2016-Jul-08"}

Date must be in the "2016-Jul-07" format

LeaveRequest.java

import java.util.Date;

public class LeaveRequest {
    private int leaveRequestId;
    private String reason;
    private Date leaveFromDate;
    private Date leaveToDate;
    private int leaveTypeId;
    private int statusId;
    private String instanceId;
    private String createdBy;
    private Date createdOn;
    private String modifiedBy;
    private Date modifiedOn;

    public LeaveRequest() {

    }

    public LeaveRequest(int leaveRequestId, String reason, Date leaveFromDate, Date leaveToDate,int leaveTypeId,int statusId, String instanceId,
            String createdBy, Date createdOn, String modifiedBy, Date modifiedOn)   {
        this.leaveRequestId=leaveRequestId;
        this.reason=reason;
        this.leaveFromDate=leaveFromDate;
        this.leaveToDate=leaveToDate;
        this.leaveTypeId=leaveTypeId;
        this.statusId=statusId;
        this.instanceId=instanceId;
        this.createdBy=createdBy;
        this.createdOn=createdOn;
        this.modifiedBy=modifiedBy;
        this.modifiedOn=modifiedOn;
    }
    public LeaveRequest(String reason, Date leaveFromDate, Date leaveToDate,int leaveTypeId,int statusId, String instanceId,
            String createdBy, Date createdOn)   {
        this.reason=reason;
        this.leaveFromDate=leaveFromDate;
        this.leaveToDate=leaveToDate;
        this.leaveToDate=leaveToDate;
        this.leaveTypeId=leaveTypeId;
        this.statusId=statusId;
        this.instanceId=instanceId;
        this.createdBy=createdBy;
        this.createdOn=createdOn;
    }
    public int getLeaveRequestId() {
        return leaveRequestId;
    }

    public void setLeaveRequestId(int leaveRequestId) {
        this.leaveRequestId = leaveRequestId;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public Date getLeaveToDate() {
        return leaveToDate;
    }

    public void setLeaveToDate(Date leaveToDate) {
        this.leaveToDate = leaveToDate;
    }

    public Date getLeaveFromDate() {
        return leaveFromDate;
    }

    public void setLeaveFromDate(Date leaveFromDate) {
        this.leaveFromDate = leaveFromDate;
    }

    public int getStatusId() {
        return statusId;
    }

    public void setStatusId(int statusId) {
        this.statusId = statusId;
    }

    public int getLeaveTypeId() {
        return leaveTypeId;
    }

    public void setLeaveTypeId(int leaveTypeId) {
        this.leaveTypeId = leaveTypeId;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public Date getCreatedOn() {
        return createdOn;
    }

    public void setCreatedOn(Date createdOn) {
        this.createdOn = createdOn;
    }

    public String getModifiedBy() {
        return modifiedBy;
    }

    public void setModifiedBy(String modifiedBy) {
        this.modifiedBy = modifiedBy;
    }

    public Date getModifiedOn() {
        return modifiedOn;
    }

    public void setModifiedOn(Date modifiedOn) {
        this.modifiedOn = modifiedOn;
    }

    public String getInstanceId() {
        return instanceId;
    }

    public void setInstanceId(String instanceId) {
        this.instanceId = instanceId;
    }

}
pramod patel
  • 115
  • 1
  • 2
  • 13

3 Answers3

1

As you said you input is 2016-07-13 which is yyyy-MM-dd format. You have to read it using yyyy-MM-dd and while writing use yyyy-MMM-dd

   ObjectMapper mapper = new ObjectMapper();
        try {
            mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
            LeaveRequest lrq = mapper.readValue("{\"leaveRequestId\":51,\"reason\":\"xdvfsgf\",\"leaveFromDate\":\"2016-07-13\",\"leaveToDate\":\"2016-07-15\",\"leaveTypeId\":9,\"statusId\":1,\"instanceId\":\"7527\",\"createdBy\":\"pramod\",\"createdOn\":\"2016-07-07\",\"modifiedBy\":null,\"modifiedOn\":null}", LeaveRequest.class);
            System.out.println(mapper.setDateFormat(new SimpleDateFormat("yyyy-MMM-dd")).writeValueAsString(lrq));
        } catch (IOException ex) {
            Logger.getLogger(NewClass1.class.getName()).log(Level.SEVERE, null, ex);
        }
Madhan
  • 5,750
  • 4
  • 28
  • 61
  • I don't think your answer is true with this case because he converts POJO object LeaveRequest to JSON String. – NguaCon Jul 08 '16 at 06:46
  • Thanks for showing a complete example of how to use the ObjectMapper object. You would be surprised how hard it was to find. – payne8 Jul 28 '17 at 06:03
1

I wrote this code to convert date to "yyyy-MM-dd" which ideally should have worked but it didn't

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setTimeZone(TimeZone.getDefault());
    ObjectMapper mapper = new ObjectMapper();
    mapper.setDateFormat(dateFormat);
    myObj =objectMapper.readValue(file,MyObj.class)

Output was coming as - "paymentDate": "2019-08-18T23:00:00.000+0000", which isnt what I was expecting..

Changed this to Declaring ObjectMapper as a Bean and then inject ObjectMapper as a dependency

    //Make sure the class is annotated with @Configuration or appropriate

  @Bean
    public ObjectMapper objectMapper() {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setTimeZone(TimeZone.getDefault());
    ObjectMapper mapper = new ObjectMapper();
    mapper.setDateFormat(dateFormat);
    return mapper;
}

and in-service class, just inject this as a dependency

@Autowired
private ObjectMapper objectMapper;

somemethod(){
  myObj =objectMapper.readValue(file,MyObj.class)
 }

Output was coming as -"paymentDate": "2019-08-19" -as expected

Abhishek Galoda
  • 2,753
  • 24
  • 38
0

Serialization and deserialization will have to come into picture when you manipulate/modify the contents as per your requirement through ObjectMapper. You can refer more about this here

This topic has also been discussed here.

Community
  • 1
  • 1
Jenifer
  • 39
  • 4