欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring boot如何配置請求的入?yún)⒑统鰠son數(shù)據(jù)格式

 更新時(shí)間:2019年11月13日 14:30:21   作者:zhangxuezhi  
這篇文章主要介紹了spring boot如何配置請求的入?yún)⒑统鰠son數(shù)據(jù)格式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了spring boot如何配置請求的入?yún)⒑统鰠son數(shù)據(jù)格式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

請求入?yún)?shù)據(jù)格式配置,以日期格式為例:

編寫日期編輯器類:

import first.zxz.tools.DateUtil;
import java.beans.PropertyEditorSupport;
/**
 * 日期編輯器
 *
 * @author zhangxz
 * @date 2019-11-12 20:01
 */
public class DateEditor extends PropertyEditorSupport {
 /**
  * 是否將null轉(zhuǎn)換為空字符串
  */
 private final boolean nullAsEmpty;
 public DateEditor() {
  this(true);
 }
 public DateEditor(boolean nullAsEmpty) {
  this.nullAsEmpty = nullAsEmpty;
 }
 @Override
 public void setAsText(String text) {
  if (text == null) {
   if (nullAsEmpty) {
    setValue("");
   } else {
    setValue(null);
   }
  } else {
   setValue(DateUtil.parse(text));
  }
 }

}

其中用到的日期工具類如下:

public class DateUtil {

 private static final String DATE_FORMAT_1 = "yyyy-MM-dd";
 private static final String DATE_FORMAT_2 = "yyyy/MM/dd";
 private static final String DATE_FORMAT_3 = "yyyyMMdd";
 private static final String DATE_TIME_FORMAT_1 = DATE_FORMAT_1 + " HH:mm:ss";
 private static final String DATE_TIME_FORMAT_2 = DATE_FORMAT_2 + " HH:mm:ss";
 private static final String DATE_TIME_FORMAT_3 = DATE_FORMAT_3 + "HHmmss";

 //key為正則表達(dá)式,value為對應(yīng)的日期格式
 private static final HashMap<String, String> DATE_REGEX_FORMATTER_MAP = new HashMap<>();

 static {
  DATE_REGEX_FORMATTER_MAP.put(DateRegexConst.DATE_1, DATE_FORMAT_1);
  DATE_REGEX_FORMATTER_MAP.put(DateRegexConst.DATE_2, DATE_FORMAT_2);
  DATE_REGEX_FORMATTER_MAP.put(DateRegexConst.DATE_3, DATE_FORMAT_3);
  DATE_REGEX_FORMATTER_MAP.put(DateRegexConst.DATE_TIME_1, DATE_TIME_FORMAT_1);
  DATE_REGEX_FORMATTER_MAP.put(DateRegexConst.DATE_TIME_2, DATE_TIME_FORMAT_2);
  DATE_REGEX_FORMATTER_MAP.put(DateRegexConst.DATE_TIME_3, DATE_TIME_FORMAT_3);
 }

 //提示語:所有可用的日期格式
 public static final String ALL_USABLE_DATE_FORMATS = DATE_REGEX_FORMATTER_MAP.values().toString() + ",以及時(shí)間戳;";

 /**
  * 根據(jù)輸入的字符串,進(jìn)行格式化,并輸入日期數(shù)據(jù)
  * 注意:格式化之前,會進(jìn)行null和空字符串過濾;格式化時(shí),會去除字符串兩端的空字符串
  *
  * @param formattedDateStr 日期字符串?dāng)?shù)據(jù)
  * @return java.util.Date
  * @author Zxz
  * @date 2019/11/6 15:07
  **/
 public static Date parse(String formattedDateStr) {
  if (formattedDateStr == null || formattedDateStr.trim().length() <= 0) {
   throw new StringEmptyException();
  }

  for (Map.Entry<String, String> entry : DATE_REGEX_FORMATTER_MAP.entrySet()) {
   if (formattedDateStr.trim().matches(entry.getKey())) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(entry.getValue());
    try {
     return simpleDateFormat.parse(formattedDateStr.trim());
    } catch (ParseException e) {
     e.printStackTrace();
     throw new DateFormatException();
    }
   }
  }

  try {
   Long aLong = Long.valueOf(formattedDateStr);
   return new Date(aLong);
  } catch (NumberFormatException e) {
   //格式不匹配
   throw new DateFormatException();
  }

 }
}

把編輯器加入web的綁定初始化配置:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.validation.Validator;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import simple.proj.zxz.play.config.editor.DateEditor;

import java.util.Date;

/**
 * 配置請求 bean數(shù)據(jù)接收格式,例如date格式
 *
 * @author zhangxz
 * @date 2019-11-12 19:56
 */

@Configuration
public class WebBindingInitializerConfig {

 /**
  * 配置請求入?yún)?shù)據(jù)格式,參考{@link simple.proj.zxz.play.config.editor.DateEditor}
  *
  * @param mvcConversionService mvc版本業(yè)務(wù)
  * @param mvcValidator   mvc校驗(yàn)器
  * @return org.springframework.web.bind.support.ConfigurableWebBindingInitializer
  * @author zhangxz
  * @date 2019/11/13 10:40
  */
 @Bean
 public ConfigurableWebBindingInitializer configurableWebBindingInitializer(FormattingConversionService mvcConversionService, Validator mvcValidator) {
  ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
  initializer.setConversionService(mvcConversionService);
  initializer.setValidator(mvcValidator);

  initializer.setPropertyEditorRegistrar(propertyEditorRegistry -> {
   //PropertyEditor 非線程安全
   propertyEditorRegistry.registerCustomEditor(Date.class, new DateEditor());
  });
  return initializer;
 }
}

入?yún)⒌娜掌跀?shù)據(jù)格式配置完成,可以接收的日期格式在日期工具類可以看到,有以下幾種:yyyy-mm-dd,時(shí)間戳,yyyy-mm-dd hh:mm:ss等。

請求出參格式配置,包括空字符串,空數(shù)組,以及日期格式化輸出

編寫空字符串序列器:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

/**
 * 空字符串序列器
 *
 * @author zhangxz
 * @date 2019-11-12 22:05
 */
public class NullStringSerializer extends JsonSerializer {

 @Override
 public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
   throws IOException, JsonProcessingException {
  jsonGenerator.writeString("");
 }
}

編寫空數(shù)組序列器

/**
 * 空數(shù)組或集合序列器
 *
 * @author zhangxz
 * @date 2019-11-12 22:04
 */

public class NullArraySerializer extends JsonSerializer {

 @Override
 public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
  jgen.writeStartArray();
  jgen.writeEndArray();
 }
}

編寫日期序列器

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.util.Date;

/**
 * 日期序列器
 *
 * @author zhangxz
 * @date 2019-11-12 23:01
 */
public class DateSerializer extends JsonSerializer {

 /**
  * 日期序列化方法,返回時(shí)間戳格式
  *
  * @param o     數(shù)值
  * @param jsonGenerator  json生成器
  * @param serializerProvider 序列器
  * @author zhangxz
  * @date 2019/11/13 10:41
  */
 @Override
 public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
  jsonGenerator.writeNumber(((Date) o).getTime());
 }
}

編寫總序列器

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;

import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 總序列器
 *
 * @author zhangxz
 * @date 2019-11-12 22:07
 */

public class GeneralSerializer extends BeanSerializerModifier {

 //空數(shù)組
 private JsonSerializer _nullArraySerializer = new NullArraySerializer();
 //空字符串
 private JsonSerializer _nullStringSerializer = new NullStringSerializer();
 //日期類型
 private JsonSerializer dateSerializer = new DateSerializer();

 @Override
 public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc,
              List<BeanPropertyWriter> beanProperties) {
  for (BeanPropertyWriter beanProperty : beanProperties) {
   if (isArrayType(beanProperty)) {
    beanProperty.assignNullSerializer(this._nullArraySerializer);
   } else if (isStringType(beanProperty)) {
    beanProperty.assignNullSerializer(this._nullStringSerializer);
   } else if (isDateType(beanProperty)) {
    beanProperty.assignSerializer(this.dateSerializer);
   }
  }
  return beanProperties;
 }

 private boolean isStringType(BeanPropertyWriter writer) {
  Class clazz = writer.getType().getRawClass();
  return clazz == String.class;
 }

 private boolean isDateType(BeanPropertyWriter writer) {
  return Date.class == writer.getType().getRawClass();
 }

 private boolean isArrayType(BeanPropertyWriter writer) {
  Class clazz = writer.getType().getRawClass();
  return clazz.isArray() || Collection.class.isAssignableFrom(clazz);
 }
}

把總序列器加入web配置中:

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import simple.proj.zxz.play.config.serializer.GeneralSerializer;

/**
 * @description: 配置jackson 序列器
 * @author: zhangxz
 * @create: 2019-11-12 22:02
 */


@Configuration
public class JacksonConfig {

 /**
  * 出參數(shù)據(jù)格式配置,參考類 {@link simple.proj.zxz.play.config.serializer.GeneralSerializer}
  *
  * @return org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
  * @author zhangxz
  * @date 2019/11/13 10:34
  */
 @Bean
 public MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter() {

  final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
  ObjectMapper mapper = converter.getObjectMapper();
  mapper.setSerializerFactory(mapper.getSerializerFactory().withSerializerModifier(new GeneralSerializer()));

  return converter;
 }
}

請求出參配置完成,出參格式如下:null的字符串輸出為空字符串,null的數(shù)組輸出為空數(shù)組,date數(shù)據(jù)格式輸出為時(shí)間戳。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解使用spring validation完成數(shù)據(jù)后端校驗(yàn)

    詳解使用spring validation完成數(shù)據(jù)后端校驗(yàn)

    這篇文章主要介紹了詳解使用spring validation完成數(shù)據(jù)后端校驗(yàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Spring中的路徑匹配器AntPathMatcher詳解

    Spring中的路徑匹配器AntPathMatcher詳解

    這篇文章主要介紹了Spring中的路徑匹配器AntPathMatcher詳解,Spring的PathMatcher路徑匹配器接口,用于支持帶通配符的資源路徑匹配,本文提供了部分實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2023-09-09
  • Java HttpClient-Restful工具各種請求高度封裝提煉及總結(jié)

    Java HttpClient-Restful工具各種請求高度封裝提煉及總結(jié)

    這篇文章主要介紹了Java HttpClient-Restful工具各種請求高度封裝提煉及總結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Java超詳細(xì)分析@Autowired原理

    Java超詳細(xì)分析@Autowired原理

    @Autowired注解可以用在類屬性,構(gòu)造函數(shù),setter方法和函數(shù)參數(shù)上,該注解可以準(zhǔn)確地控制bean在何處如何自動裝配的過程。在默認(rèn)情況下,該注解是類型驅(qū)動的注入
    2022-06-06
  • Spring注解實(shí)現(xiàn)自動裝配過程解析

    Spring注解實(shí)現(xiàn)自動裝配過程解析

    這篇文章主要介紹了Spring注解實(shí)現(xiàn)自動裝配過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Java解決代碼重復(fù)的三個(gè)絕招分享

    Java解決代碼重復(fù)的三個(gè)絕招分享

    本文將從業(yè)務(wù)代碼中最常見的三個(gè)需求展開,聊聊如何使用?Java?中的一些高級特性、設(shè)計(jì)模式,以及一些工具消除重復(fù)代碼,才能既優(yōu)雅又高端
    2022-07-07
  • Java學(xué)習(xí)關(guān)于循環(huán)和數(shù)組練習(xí)題整理

    Java學(xué)習(xí)關(guān)于循環(huán)和數(shù)組練習(xí)題整理

    在本篇文章里小編給各位整理了關(guān)于Java學(xué)習(xí)關(guān)于循環(huán)和數(shù)組練習(xí)題相關(guān)內(nèi)容,有興趣的朋友們跟著參考學(xué)習(xí)下。
    2019-07-07
  • 關(guān)于Spring?Cloud的熔斷器監(jiān)控問題

    關(guān)于Spring?Cloud的熔斷器監(jiān)控問題

    Turbine是一個(gè)聚合Hystrix監(jiān)控?cái)?shù)據(jù)的工具,它可將所有相關(guān)/hystrix.stream端點(diǎn)的數(shù)據(jù)聚合到一個(gè)組合的/turbine.stream中,從而讓集群的監(jiān)控更加方便,接下來通過本文給大家介紹Spring?Cloud的熔斷器監(jiān)控,感興趣的朋友一起看看吧
    2022-01-01
  • java迭代器基礎(chǔ)知識點(diǎn)總結(jié)

    java迭代器基礎(chǔ)知識點(diǎn)總結(jié)

    在本篇內(nèi)容里小編給大家整理了一篇關(guān)于java迭代器基礎(chǔ)知識點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。
    2021-01-01
  • JPA原生SQL(自定義SQL)分頁查詢邏輯詳解

    JPA原生SQL(自定義SQL)分頁查詢邏輯詳解

    這篇文章主要介紹了JPA原生SQL(自定義SQL)分頁查詢邏輯詳解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11

最新評論