In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "SpringBoot how to use Jackson to return JSON data date format", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "SpringBoot how to use Jackson to return JSON data date format" bar!
@ Bean@Primary@ConditionalOnMissingBean (ObjectMapper.class) public ObjectMapper jacksonObjectMapper (Jackson2ObjectMapperBuilder builder) {ObjectMapper objectMapper = builder.createXmlMapper (false) .build () / / set the mapper object through this method, and all serialized objects will be serialized according to the change rule / / Include.Include.ALWAYS default / / Include.NON_DEFAULT property is the default value / / Include.NON_EMPTY property is empty ("") or NULL is not serialized, then the returned json does not have this field. This will save more traffic on the mobile side / / Include.NON_NULL attribute is NULL do not serialize objectMapper.setSerializationInclusion (JsonInclude.Include.NON_EMPTY); objectMapper.configure (DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); / / allow special characters and escape characters objectMapper.configure (JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true) / / single quotation marks objectMapper.configure (JsonParser.Feature.ALLOW_SINGLE_QUOTES, true) are allowed / / keep the null value to "" objectMapper.getSerializerProvider () .setNullValueSerializer (new JsonSerializer () {@ Override public void serialize (Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {jsonGenerator.writeString (");}})) Return objectMapper } / / https://www.cnblogs.com/liaojie970/p/9396334.htmlspring: jackson: # date format date-format: yyyy-MM-dd HH:mm:ss serialization: # format output indent_output: true # ignore unconvertible objects fail_on_empty_beans: false # set empty how to serialize defaultPropertyInclusion: NON_EMPTY deserialization: # allow objects to ignore attributes that do not exist in json fail_on_unknown_properties: false parser: # allow special characters and escape characters allow_unquoted_control_chars: true # allow single quotation marks allow_single_quotes: true/** * test date * @ param joinDay * @ return * / @ RequestMapping ("date") Public Date testDate (Date joinDay) {return joinDay } package com.streamyear.course.config; import org.springframework.core.convert.converter.Converter;import org.springframework.stereotype.Component; import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List; / * global page passes a date string, which is automatically converted to date format * / @ Componentpublic class DateConverterConfig implements Converter {private static final List formarts = new ArrayList (4) Static {formarts.add ("yyyy-MM"); formarts.add ("yyyy-MM-dd"); formarts.add ("yyyy-MM-dd hh:mm"); formarts.add ("yyyy-MM-dd hh:mm:ss");} @ Override public Date convert (String source) {String value = source.trim () If (".equals (value)) {return null;} if (source.matches (" ^\ d {4} -\ d {1je 2} $")) {return parseDate (source, formarts.get (0)) } else if (source.matches ("^\\ d {4} -\ d {1Magne2} -\\ d {1jin2} $") {return parseDate (source, formarts.get (1)) } else if (source.matches ("^\\ d {4} -\ d {1 formarts.get 2} -\\ d {1} 2}:\\ d {1J 2}:\) {return parseDate (source, formarts.get (2)) } else if (source.matches ("^\\ d {4} -\ d {1else if 2} {1}\ d {1Power2}:\\ d {1Power2}:\) {return parseDate (source, formarts.get (3));} else {throw new IllegalArgumentException (" Invalid boolean value'"+ source +")) }} / * format date * @ param dateStr String character date * @ param format String format * @ return Date date * / public Date parseDate (String dateStr, String format) {Date date=null; try {DateFormat dateFormat = new SimpleDateFormat (format); date= dateFormat.parse (dateStr) } catch (Exception e) {} return date;}} @ Configurationpublic class JacksonConfiguration {@ Bean public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter () {MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter (); ObjectMapper objectMapper = new ObjectMapper (); / / ignore the unrecognized attribute objectMapper.configure (DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) in the json string / ignore the unconvertible object "No serializer found for class com.xxx.xxx" objectMapper.configure (SerializationFeature.FAIL_ON_EMPTY_BEANS, false); / / NULL does not participate in serialization / / objectMapper.setSerializationInclusion (Include.NON_NULL); / / PrettyPrinter format output objectMapper.configure (SerializationFeature.INDENT_OUTPUT, true) / / specify time zone, default UTC, instead of jvm default time zone objectMapper.setTimeZone (TimeZone.getTimeZone ("GMT+8:00")); / / date type handling objectMapper.setDateFormat (new SimpleDateFormat (DateUtil.DEFAULT_FORMAT_DATETIME)); converter.setObjectMapper (objectMapper); return converter Convenient implementation of / * BeanPostProcessor to perform method-level verification on annotated methods * Note: roommate @ Validated annotations need to be annotated on the target class in order to search for the method of inline constraint annotations * A convenient BeanPostProcessor implementation * that delegates to a JSR-303 provider for performing method-level validation on annotated methods * @ return * / @ Bean public MethodValidationPostProcessor methodValidationPostProcessor () {return new MethodValidationPostProcessor () }} Thank you for your reading, the above is the content of "how SpringBoot uses Jackson to return JSON data date format". After the study of this article, I believe you have a deeper understanding of how SpringBoot uses Jackson to return JSON data date format, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.