springboot配置fastjson 解析---- 返回json字符串 null转空字符串解决方案

Springboot中解析JSON字符串(jackson库ObjectMapper解析JSON字符串) springboot中通过jackson库中的objectmapper对象实现,对象和JSON相互转化,List集合JSON相互转化,Map和JSON相互转化 阅读详情

springboot默认采用的是jackson解析json,在未进行配置的情况下会出现值为空的键丢失的情况,网上有配置的方案,但不能解决全部问题,这里采用更换json解析工具的解决办法,

引入fastjson jar包

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>${fastjson.version}</version>
</dependency>

添加configbean

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;

@Configuration
public class BeanConfig {

    /*注入Bean : HttpMessageConverters,以支持fastjson*/
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastConvert = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.DisableCheckSpecialChar);
        fastJsonConfig.setDateFormat("yyyy-MM-dd hh:mm:ss");
        //处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConvert.setSupportedMediaTypes(fastMediaTypes);
        fastConvert.setFastJsonConfig(fastJsonConfig);
        return new HttpMessageConverters((HttpMessageConverter<?>) fastConvert);
    }
}

fastjon 的详细配置 在这篇文章中讲的很详细,就不一 一叙述了
http://blog.csdn.net/u010246789/article/details/52539576

【第一篇】Xshell连接服务器部署前后端项目(连接服务器) 利用Xshell连接服务器部署前后端项目 阅读详情

相关推荐

哈密顿-雅可比不等式:连接最优控制与优化算法的自然物理原理

在控制理论与优化算法之间,存在一个深刻而实用的桥梁。最优控制理论旨在通过设计控制输入,使动态系统在时间维度上达到性能最优,其核心数学工具如庞特里亚金极大值原理和哈密顿-雅可比-贝尔曼方程,为解决连续时间系统的优化问题提供了优雅框架。然而,这些方法在高维非凸问题中面临求解难题。哈密顿-雅可比不等式作为一种松弛方法,通过构造李雅普诺夫函数并验证一个不等式条件,转而寻求性能有保障且易于实现的次优解,其本质是保证系统能量衰减和性能上界。这一原理与耗散性理论紧密相连,体现了自然物理系统(如阻尼振动)中能量耗散的普遍规

weixin_30582943的博客 318

springboot返回给前端数据将null值转化为空字符串

springboot返回给页面的json数据中,如果有数据为null,则返回空字符串。 springboot默认使用jackson解析返回json数据。 我们做一下配置: package com.yhcode.config; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.

nyasm的博客 3万+

BLDC方波控制 vs. FOC:技术选型指南与未来演进

本文深入对比BLDC电机方波控制与FOC技术的核心差异,分析两者在成本、性能和应用场景上的权衡。方波控制凭借低成本和易实现性,在电动工具和家电领域优势明显;而FOC则以平滑高效控制见长,适用于高端精密应用。文章为工程师提供了实用的技术选型指南,并展望了未来技术融合的发展趋势。

yhn4567890的博客 1175

com.alibaba.fastjson处理datetime为null的数据的解决办法

情景:使用fastjson返回数据时,时间为空时返回null,并没有出现想要的空串,前端读取数据有些问题,查看FastjsonConverter类发现没有对时间类型没有进行空值过滤处理,在网上找了一些发现记录解决此问题很少,解决后特在此记录一下,供需要的产考一下. 未解决之前代码如下: package com.seeker.config; import java.nio.charset.C...

L-seeker-Q的博客 2185

activiti(六):activiti7的数据表和字段的解释

介绍activiti7的数据表和各表字段的含义,对于学习activiti7工作流引擎的运行过程很有帮助!

no problem的博客 1万+

ptrade从零开始学量化第1期【ptrade如何新建策略、新建回测、新建交易】什么是ptrade量化交易?

回测之前需要对开始时间、结束时间、回测资金、回测基准、回测频率几个要素进行设定,设定完毕后点击保存。然后再点击回测按键,系统就会开始运行回测,回测的评价指标、收益曲线、日志都会在界面中展现。可以选择不同的业务类型(比如股票),然后给策略设定一个名称,添加成功后可以在默认策略模板基础上进行策略编写。交易界面点击新增按键进行新增交易操作,策略方案中的对象为所有策略列表中的策略,给本次交易设定名称并点击确定后系统就开始运行交易了。交易开始运行后,可以点击交易详情,查看策略评价指标、交易明细、持仓明细、交易日志。

GJ57017_wx的博客 2496

返回值中有null导致fastjson解析异常?

昨天在做接口调试的时候遇到了一个问题,返回值一直报这个错,经过多层的log.info才定位到问题是因为返回值中有一个null导致了JSON.toJSONString(result))报错。

零落的尘土 1107

2020年大连理工大学《计算机控制技术》期末试卷

2020年大连理工大学《计算机控制技术》期末试卷 2020年大连理工大学《计算机控制技术》期末试卷 2020年大连理工大学《计算机控制技术》期末试卷 2020年大连理工大学《计算机控制技术》期末试卷 ...

无名何许人的博客 653

SpringBoot中返回数据序列化NULL值处理

返回数据给前端的时候,不同的数据类型的NULL值我们想序列化成默认值进行输出

dangzhenle的博客 4176

基于深度学习的工业零件缺陷检测系统

工业零件缺陷检测系统摘要 本文介绍了一个基于深度学习的工业零件缺陷检测系统解决方案。该系统采用Faster R-CNN目标检测算法,结合PyTorch框架和OpenCV图像处理库,实现了对工业零件表面缺陷(如裂纹、划痕、凹坑等)的自动检测与定位。系统包含完整的开发流程:从数据采集与标注、数据增强预处理、模型构建与训练,到最终的评估与部署。相比传统机器视觉方法,该深度学习方案具有更强的特征表示能力和环境适应性,能够有效提升工业质检的准确率和效率。文章详细阐述了系统架构设计、实现细节和优化策略,并提供了完整的代

huanghm88的专栏 292

通用抓取————6D位姿估计

这意味着算法在没有物体的先验知识(如 CAD 模型)的情况下,依然能够通过神经网络捕捉物体的外观、形状和几何特征,生成相关的 6D 位姿估计。通过输入物体类别的描述,算法能够推断出该类别物体的位姿和尺度,无需特定物体的 CAD 模型。在这个坐标空间中,物体的位姿和尺度都是已知的,基于这个标准化表示,算法可以对输入图像中的物体进行 6D 位姿和尺寸的估计。○优势: ICP 是一种非常有效的位姿优化算法,特别适合精细调整物体的位姿,确保最终估计的位姿与真实物体的点云或模型高度一致。

具身机器人和通用AI 2398

fastjson null 值转换为空字符串 key值统一大小写

fastjson 可以设置null值转为 SerializerFeature.WriteNullStringAsEmpty 但是这个属性只针对实体,对map这种结构不起效 此时需要如下所示,会将全部的null值转换为空字符串 但是特别注意,null值的时候丢失了原数据类型,所以不管数字,日期,或者其他的都变成了"" 所以如果有可能还是不要偷懒,尽量用实体 /* ...

a1091662876的博客 1584

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

SpringMvc中返回json时对象属性为空也要返回key

场景: 最近在写接口时候,正常使用@RestController返回json串,发现当返回的对象里的属性值为空字符串或者null时候,json返回里就会自动去除这个key,啥意思呢?举个"栗子": /** * 商品评论、回复、点赞【查询】接口 * * @param goodsId * @param userId * @param currenP...

分享传递价值 8723

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

springboot返回前端对象null转为空字符串

在测试移动端接口的时候,手机端开发的人要求我们把返回对象null转成空字符串,觉得页面显示null不美观。虽然我让他们在手机端判断一下就好,不过他们很不情愿的样子,实际上就是懒。算了,我就上网查找了一下资料 import java.io.IOException; import org.springframework.boot.autoconfigure.condition.Condition...

qq_36804701的博客 3万+

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

FastJson 过滤/保留 NULL值问题

FastJson 过滤/保留 NULL值问题FastJson介绍 FastJson介绍 fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean 最近项目中用到了FastJson,但是在操作的时候发现fastJson会对null值进行自动过滤,但是项目中需要保留null值,所以写...

glei20的博客 2万+

fastjson序列化将Null设置为空字符串""等问题

fastjson序列化将Null设置为空字符串""等问题 Spring Boot使用FastJson解析JSON数据的方法 Spring Boot默认使用json格式从后台返回数据给前台,当返回的String类型或者List&lt;&gt;类型数据为null时,我们希望返回的是""或者[],此时就需要使用fastjson来转换。使用fastjson需要注意以下事项: 1、加入fastjson依赖项...

清风皓月长歌 1万+

Springboot 通过FastJson实现bean对象和Json字符串互转

Json串我们使用场景1的输出,但是稍作修改:删掉adminUser,同时注意userBeanList容器中的元素,两个id的key不一样,一个叫做userId(类字段名),一个叫做user-id(JSONField的name值)。注意看,(防盗连接:本文首发自http://www.cnblogs.com/jilodream/ )如果model类字段设置了JSONField注解的话,则优先级最高,如果未设置该注解的话,则按照model类中字段的名称进行设置。而基本类型、复杂类型、复杂类型容器等场景均支持。

m0_71777195的博客 2976

hutol json null值没了_配置使用FastJson返回Json视图

fastJson是阿里巴巴旗下的一个开源项目之一,顾名思义它专门用来做快速操作Json的序列化与反序列化的组件。它是目前json解析最快的开源组件没有之一!在这之前jaskJson是命名为快速操作json的工具,而当阿里巴巴的fastJson诞生后jaskjson就消声匿迹了,不过目前很多项目还在使用。本章目标将fastJson加入到SpringBoot项目内,配置json返回视图使用fastJs...

weixin_35924719的博客 265

clob超过4000如何转字符串_SpringBoot 项目开发是如何返回 json 数据以及数据封装

本文目录如下:1. Spring Boot 默认对Json的处理1.1 创建 User 实体类1.2 创建Controller类1.3 测试不同数据类型返回的json1.4 jackson 中对null的处理2. 使用阿里巴巴FastJson的设置2.1 jackson 和 fastJson 的对比2.2 fastJson依赖导入2.2 使用 fastJson 处理 null3. 封装统一返回的数...

weixin_39906521的博客 745

SpringBoot返回Json数据

{“code”:“0”, “data”:{“作者信息”:{“id”:1, “password”:“”,“username”:“闫峻”},“CSDN 地址”:null,“粉丝数量”:4153,“博客地址”:“http://blog.yan.com”},“msg”:“操作成功!{“作者信息”:{“id”:1,“username”:“闫峻”,“password”:“”},“CSDN 地址”:“”,“粉丝数量”:4153,“博客地址”:“http://blog.yan.com”}

qq_40067747的博客 1184

SpringBoot2.1配置FastJson

使用FastJson处理返回数据,默认不会返回数据为null的属性,这对前端进行编码的时候会造成一些困扰,但是通过配置FastJson可以解决这个困扰。 PS: 1、 @Configuration 该注解使得该类被SpringBoot启动加载 configureMessageConverters 该方法是解析返回数据用到的方法,所以需要重写 通过该类的处理,返回的json数据中如果包含集合为n...

1119

SpringBoot 之控制器的返回值处理

@Controller:默认返回值是视图名称,如果需要返回Json字符串,需要在方法上加@ResponseBody注解,默认使用 Jackson 框架处理。 @RestController:是@ResponseBody和@Controller的组合注解。 Spring Boot 中默认使用的 Json 解析框架是 Jackson 。 在实际项目中,难免会遇到一些 null 值出现,在转 Json 时,是不希望有这些 null 出现的。 比如期望所有的 null 在转 Json 时都变成 “” 这种空字符串。

const_ 1374

.net core json 为null输出_002、Spring Boot返回Json数据及数据封装

在项目开发中,接口与接口之间,前后端之间数据的传输都使用 Json 格式,在 Spring Boot 中,接口返回 Json 格式的数据很简单,在 Controller 中使用@RestController注解即可返回 Json 格式的数据,@RestController也是 Spring Boot 新增的一个注解,我们点进去看一下该注解都包含了哪些东西。@Target可以看出, @RestCon...

weixin_39969298的博客 121

FastJson 过滤/保留 NULL值问题,在远程调用过程传输中过滤了null值导致前端没有key

简单的数据好像可以这样解决 Map<String, Object> map = JSONObject.parseObject(respJson, new TypeReference<Map<String, Object>>() { }); 这样可以转化为一个有null值的map,这样就不用考虑值为null的key被抹除的问题了(太坑了!) FastJson介绍 fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为

yubin1285570923的博客 1526

重学springboot系列之JSON处理工具类:FastJSON、Gson和Jackson对比,Json常用注解,手动数据转换,全局配置:使用配置文件和代码,hutool工具解析json

重学springboot系列之JSON处理工具类:FastJSON、Gson和Jackson对比,Json常用注解,手动数据转换,全局配置:使用配置文件和代码,hutool工具解析json

qq120631157的博客 4040
上一篇: 解决Tomcat文件过大问题
下一篇: MySql自动插入当前时间
ZPHTTT
博客等级 码龄9年 77粉丝 23原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值