Springboot接收前端的Json但是為null問(wèn)題
Springboot接收前端的Json但是為null
接收前端傳過(guò)來(lái)的Json,
{ "id": 94, "nickname": "王明", "username": "蕭強(qiáng)", "password": "nulla", "email": "p.tplfsayobt@qq.com", "avatar": "http://dummyimage.com/100x100", "type": 47, "createTime": "2007-12-03 08:23:55", "updateTime": "1972-02-26 05:02:56" }
然后反序列化轉(zhuǎn)化成User實(shí)體類(lèi),打印實(shí)體類(lèi)為空,仔細(xì)檢查了一下。
@PostMapping(value = "/register") public Result registerUser(User user) { userService.register(user); return ResultGenerator.getSuccessResult(user); }
少了@RequestBody注解
@PostMapping(value = "/register") public Result registerUser(@RequestBody User user) { userService.register(user); return ResultGenerator.getSuccessResult(user); }
現(xiàn)在實(shí)體類(lèi)就有數(shù)據(jù)了。
Springboot將返回前端的null值變?yōu)楣潭ㄖ?/h2>
做了個(gè)項(xiàng)目 當(dāng)我給前端返回null值時(shí)需求要把 null值都返回成 “–” 記錄一下子
在springboot上配置一下就好
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializerProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import java.io.IOException; /** * @author : Nan * @date : 2021/06/20 * @description : */ @Configuration public class JacksonConfig { @Bean @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = builder.createXmlMapper(false).build(); objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() { @Override public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, IOException { // 這里可以給任意值 將返回null的值替換 但是目前不知道還想返回null怎么辦 jsonGenerator.writeString("--"); } }); return objectMapper; } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- springboot 如何解決static調(diào)用service為null
- SpringBoot中@Autowired 失效及@Autowired 注入為null的解決
- SpringBoot使用@NotEmpty、@NotBlank、@NotNull注解進(jìn)行參數(shù)校驗(yàn)
- Springboot?Filter中注入bean無(wú)效為null問(wèn)題
- Springboot編寫(xiě)CRUD時(shí)訪問(wèn)對(duì)應(yīng)數(shù)據(jù)函數(shù)返回null的問(wèn)題及解決方法
- SpringBoot AOP導(dǎo)致service注入后是null的問(wèn)題
相關(guān)文章
Spring Boot + Vue 基于 RSA 的用戶身份
RSA是一種非對(duì)稱加密算法,適用于用戶身份認(rèn)證加密,本文介紹了基于RSA的用戶身份認(rèn)證加密機(jī)制的實(shí)現(xiàn),包括前端Vue.js使用jsencrypt庫(kù)對(duì)用戶名密碼進(jìn)行加密,后端使用RSA私鑰解密驗(yàn)證用戶憑據(jù),感興趣的朋友跟隨小編一起看看吧2024-11-11基于SpringBoot與Mybatis實(shí)現(xiàn)SpringMVC Web項(xiàng)目
這篇文章主要介紹了基于SpringBoot與Mybatis實(shí)現(xiàn)SpringMVC Web項(xiàng)目的相關(guān)資料,需要的朋友可以參考下2017-04-04break和continue的作用和區(qū)別解析(案例分析)
break和continue都是用來(lái)控制循環(huán)結(jié)構(gòu)的,主要作用是停止循環(huán),這篇文章主要介紹了break和continue的作用和區(qū)別,需要的朋友可以參考下2023-03-03httpclient的disableConnectionState方法工作流程
這篇文章主要為大家介紹了httpclient的disableConnectionState方法工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Java并發(fā)編程之Executors類(lèi)詳解
今天給大家?guī)?lái)的是關(guān)于Java并發(fā)編程的相關(guān)知識(shí),文章圍繞著Java Executors類(lèi)展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06