1. 程式人生 > >spring / spring boot 使用@responseBody 過濾向前臺傳值null

spring / spring boot 使用@responseBody 過濾向前臺傳值null

Spring配置檔案中新增:<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper">
                <bean class="com.fasterxml.jackson.databind.ObjectMapper"
>
<property name="serializationInclusion"> <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value> </property> </bean> </property> </bean
>
</mvc:message-converters> </mvc:annotation-driven>
--------------------------------------------------------------------------------------------------------Spring boot 新增配置類:import java.io.IOException; 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 com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializerProvider; /**   * 〈返回json空值去掉null和""〉 〈功能詳細描述〉   *    * @version 2017年10月13日   */   @Configuration   public class JacksonConfig   {       @Bean       @Primary       @ConditionalOnMissingBean(ObjectMapper.class)       public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder)       {           ObjectMapper objectMapper = builder.createXmlMapper(false).build();              // 通過該方法對mapper物件進行設定,所有序列化的物件都將按改規則進行系列化           // Include.Include.ALWAYS 預設           // Include.NON_DEFAULT 屬性為預設值不序列化           // Include.NON_EMPTY 屬性為 空("") 或者為 NULL 都不序列化,則返回的json是沒有這個欄位的。這樣對移動端會更省流量           // Include.NON_NULL 屬性為NULL 不序列化         //  objectMapper.setSerializationInclusion(Include.NON_NULL);              // 欄位保留,將null值轉為""           objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>()       {           @Override           public void serialize(Object o, JsonGenerator jsonGenerator,                                 SerializerProvider serializerProvider)                   throws IOException, JsonProcessingException           {               jsonGenerator.writeString("");           }       });           return objectMapper;       }   }  

相關推薦

spring / spring boot 使用@responseBody 過濾前臺null

Spring配置檔案中新增:<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="org.spring

spring mvc ModelAndView前臺

今天在做專案的時候遇到一個問題,把第一個頁面儲存的id傳到第三個頁面中去用,原來是在controller層加了一個全域性變數控制的,但是後來發現這個變數實現不了我要的功能,於是查了一下,原來ModelAndView這個類有個構造方法可以傳引數到前臺,最後問題解決。 Mode

MVC 後臺前臺,同一Controller下Action之間的,Controller與Controller之間的

login href text htm 密碼 log pos 前臺 html @{ Layout = "~/Views/Shared/_Layout.cshtml";} <h2>登錄</h2> <p> @using(Html.

java 後臺list轉換成json前臺

通常前臺js需要對後臺傳過來的值進行解析,如果後臺向前臺傳入的是一個json串的話,js比較容易處理 後臺 //根據自己需求寫一個list List<Object> list = _assistAdpater.selectAllByMapParam(

關於使用JOSN轉換java物件或集合中遇到日期格式問題(或後臺前臺,日期格式問題解決辦法)

1,新建工具類:DateToJsonUtil    程式碼如下: public class DateToJsonUtil implements JsonValueProcessor { private String format = null; pub

struts2中用session進行後臺前臺

JSP頁面程式碼 <td align="right"><label for="xm1">姓名:</label></td> <td> <s:textfield name="xm" valu

Struts2+spring+Mybatis,前臺後臺亂碼的問題

Struts2+spring+Mybatis,前臺向後臺傳值亂碼的問題    1)在頁面上定義charset的字符集      <%@ page language="Java"  pag

Spring MVC 頁面 Map、Model和ModelMap

內容 ros obj model clas mod div java mode 除了使用ModelAndView方式外。還可以使用Map、Model、ModelMap來向前臺頁面創造 使用後面3種方式,都是在方法參數中,指定一個該類型的參數。例如: java類中 @Req

Spring MVC 從 Controller頁面的方式

用戶 () 傳參數 control let att model enter 設定 Spring MVC 從 Controller向頁面傳值的方式 在實際開發中,Controller取得數據(可以在Controller中處理,當然也可以來源於業務邏輯層),傳給頁面,常用的方

前臺後臺的兩種方法 以及 從後臺獲取數據的方法

list 獲取數據 data border asc req rip attr 訂單 1、前臺向後臺傳值方法一: 表單提交的方式: 1 <form action="${path }/manage/logon.do" method="post" id="logonFo

如何實現後臺前臺資料 如何實現後臺前臺資料

如何實現後臺向前臺傳資料   技術交流群:233513714       這兩天正在研究如何讓後天主動向前臺展現資料,只要後臺有資料上傳的時候就向前臺上傳(因為公司有個專案,硬體裝置會不斷的上傳資料,服務

如何實現後臺前臺數據

ron modules 接收 ini dwr targe art 調用 n) 技術交流群:233513714 這兩天正在研究如何讓後天主動向前臺展現數據,只要後臺有數據上傳的時候就向前臺上傳(因為公司有個項目,硬件設備會不斷的上傳數據,服務端將接收到的

後臺前臺反斜槓、單引號等特殊字元問題

後臺action中向前臺返回的值包含反斜槓、單引號等特殊字元,在前臺用el表示式或者java程式碼取值報錯,如: 後臺:request.setAttribute("name","\\"); 前臺取值:'${name}'或‘<%=request.getParameter

jquery ajax後臺前臺list 前臺用jquery $.each遍歷list

Js程式碼   $.ajax({       type: 'post',       url: xxx.action',       dataType: 'text',       success: function(data){                       var dataObj=

java前臺後臺中文亂碼問題解決

1:從jsp頁面向後臺傳值 jsp:<a href="<%/path%>/kqbg?search<%= URLEncoder.encode("口腔","gb2312")%>"></a> java: String str=req

Java-關於前臺jsp後臺

 關於這個問題在群裡,百度各種請教,搜尋,但是苦於無人搭理,而且百度大神也不是萬能的,終於請教了現實中一位大神,解決了此類問題,很簡單,下面做一個小記錄。首先jsp頁面需要一個引數來傳遞,引數是的變

springmvc 前臺後臺的兩種方式

<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/j

Spring mvc 關於後臺DATE型別接受的問題

如果bean為的型別為 java.util.DATE這我們可以引入註解@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd hh:mm:ss") ti

ajax controller

bind ets elements 必須 api stp light cnblogs into 第一種傳值: controller中是幾個單獨的基本類型參數 @RequestMapping("update") @ResponseBody//此註解不能省略 否則ajax

jquery $.post() php 實現簡單的二級聯動

chang var box jquer lec 簡單 cnblogs encode can 1 其中selectid是一個下拉菜單的id 2 3 $().ready(function () { 4 $("#selectid").change(functi