Home > Back-end >  About the date to JSON
About the date to JSON

Time:11-24

date directly json
 
@ RequestMapping (value="https://bbs.csdn.net/json4", produces="application/json; Charset=utf-8 ")//String format conversion produces="text/HTML. Charset=utf-8 "
@ ResponseBody
Public String json4 () throws JsonProcessingException {

ObjectMapper mapper=new ObjectMapper ();

Date date=new Date();

Return mapper. WriteValueAsString (date);
}


with show figure, you can see the returned is a timestamp, let's to * *



after the improvement:
 
@ RequestMapping (value="https://bbs.csdn.net/json4", produces="application/json; Charset=utf-8 ")//String format conversion produces="text/HTML. Charset=utf-8 "
@ ResponseBody
Public String json4 () throws JsonProcessingException {

ObjectMapper mapper=new ObjectMapper ();
//in order not to return to the timestamp, so we choose to shut it down
Mapper. The configure (SerializationFeature WRITE_DATES_AS_TIMESTAMPS, false);
//custom date format issue
Final SimpleDateFormat SDF=new SimpleDateFormat (" YYYY - MM - DD HH: MM: SS ");
//set the mapper specified date format
Mapper. SetDateFormat (SDF);

Date date=new Date();

Return mapper. WriteValueAsString (date);
}




I sent two articles about json can see too much code duplication, and throws an exception, so I will be in my next blog to encapsulate utility class.

CodePudding user response:

Have shortcomings, feel free to point out that
  • Related