Spring boot如何配置请求的入参和出参json数据格式
作者:zhangxuezhi 时间:2023-10-23 17:27:48
这篇文章主要介绍了spring boot如何配置请求的入参和出参json数据格式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
请求入参数据格式配置,以日期格式为例:
编写日期编辑器类:
import first.zxz.tools.DateUtil;
import java.beans.PropertyEditorSupport;
/**
* 日期编辑器
*
* @author zhangxz
* @date 2019-11-12 20:01
*/
public class DateEditor extends PropertyEditorSupport {
/**
* 是否将null转换为空字符串
*/
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为正则表达式,value为对应的日期格式
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() + ",以及时间戳;";
/**
* 根据输入的字符串,进行格式化,并输入日期数据
* 注意:格式化之前,会进行null和空字符串过滤;格式化时,会去除字符串两端的空字符串
*
* @param formattedDateStr 日期字符串数据
* @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数据接收格式,例如date格式
*
* @author zhangxz
* @date 2019-11-12 19:56
*/
@Configuration
public class WebBindingInitializerConfig {
/**
* 配置请求入参数据格式,参考{@link simple.proj.zxz.play.config.editor.DateEditor}
*
* @param mvcConversionService mvc版本业务
* @param mvcValidator mvc校验器
* @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;
}
}
入参的日期数据格式配置完成,可以接收的日期格式在日期工具类可以看到,有以下几种:yyyy-mm-dd,时间戳,yyyy-mm-dd hh:mm:ss等。
请求出参格式配置,包括空字符串,空数组,以及日期格式化输出
编写空字符串序列器:
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("");
}
}
编写空数组序列器
/**
* 空数组或集合序列器
*
* @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 {
/**
* 日期序列化方法,返回时间戳格式
*
* @param o 数值
* @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 {
//空数组
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 {
/**
* 出参数据格式配置,参考类 {@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的数组输出为空数组,date数据格式输出为时间戳。
来源:https://www.cnblogs.com/zhangxuezhi/p/11847773.html
标签:spring,boot,配置,请求,参,json
0
投稿
猜你喜欢
使用C#实现读取系统配置文件的代码实例讲解
2023-02-18 10:36:56
Java并发控制机制详解
2022-12-12 22:07:56
Java实现驼峰、下划线互转的方法
2023-08-18 09:17:54
解决Spring国际化文案占位符失效问题的方法
2022-10-20 23:42:23
Java C++题解leetcode904水果成篮
2023-12-08 21:06:09
java 中设计模式(值对象)的实例详解
2021-12-27 14:15:08
ref与out之间的区别深入解析
2023-08-05 23:45:28
Unity实现游戏存档框架
2023-02-10 13:05:15
Java两种方法计算出阶乘尾部连续0的个数
2021-09-03 06:04:40
C语言实现学生信息管理系统
2023-05-24 12:08:50
Android12 蓝牙适配的实现步骤
2021-08-25 08:36:34
C#自定义控件实现TextBox禁止粘贴的方法
2023-08-03 06:00:22
Java获取视频时长、大小的示例
2023-01-16 01:48:44
Java使用Jdbc连接Oracle执行简单查询操作示例
2021-07-25 03:34:25
使用mybatis插件PageHelper实现分页效果
2023-03-29 15:12:03
Spring Core动态代理的实现代码
2021-12-11 03:40:54
Java实现经典游戏Flappy Bird的示例代码
2022-03-28 07:05:37
java isInterrupted()判断线程的实例讲解
2023-07-21 01:45:53
Java 中解决Unsupported major.minor version 51.0的问题
2022-07-22 03:53:08
java微信红包实现算法
2023-08-09 00:19:39