導航:首頁 > 編程語言 > springjson格式化

springjson格式化

發布時間:2023-03-21 19:56:42

Ⅰ Spring Boot使用@jsonProperty,@JsonIgnore,@JsonFormat註解

@JsonProperty, @JsonIgnore 和 @JsonFormat 註解都是 fasterxml jackson 裡面的註解,現在也被 Spring Boot 集成了。
這里需要注意的是將對象轉換成json字元串使用的方法是fasterxml.jackson提供的!!
如果使用fastjson
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
沒有生效,為啥?
因為fastjson不認識@JsonProperty註解呀!所以要使用jackson自遲埋己的序列化工具方法

我們在使用上面的註解時,不需要在 pom.xml 顯示的引入 fasterxml jackson 的依賴包。只需要加入如下依賴即可。

@JsonProperty
用於屬性、setter / getter 方法上,屬性序列化後可重命名

生成的 json 字元串就是image_width和image_height。
@JsonIgnore

屬性使用此註解後,將不被序列化。

@JsonFormat
用於格式化日期

@JsonInclude,@JsonIgnoreProperties,@JsonIgnore

真實案例
{
"rowid": "111111",
"created": "液清2018-12-27 16:15:25",
"createdby": "1111111",
"lastupd": "2018-12-27 08:25:48",
"lastupdby": "111111",
"modificationnum": 1
}
返回Json參數欄位均為小寫,在接收時,需要按照標準的命名規則進行映射

解決辦法:

創建接收數據對象,生成Get\Set方法:,在Set方法上,加上@JsonProperty註解,

@JsonProperty 此註解用於屬性上,作用是把該屬性的名稱序列化為另鬧旦前外一個名稱,如把rowId屬性序列化為rowid,@JsonProperty("rowid")。

Ⅱ SpringMVC項目如何全局格式化日期格式

spring的RequestMappingHandlerAdapter中的messageConverters就能夠全局格式化日期格式

1. 配置spring-servlet
- 引入請求映射器
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
- 加入消息轉化器
messageConverters
- 引入Json轉化器
org.springframework.http.converter.json.
- 實現對象轉化器
objectMapper
- 日期轉換格式定義
dateFormat
2. 代碼
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.suixingpay.common.web.WebBindingInitializer">
<property name="conversionService">
<bean class="org.springframework.format.support."></bean>
</property>
</bean>
</property>
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean class="org.springframework.http.converter.json.">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
</bean>
</property>
<property name="serializationInclusion">
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
</property>
</bean>
</property>
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>text/plain;charset=UTF-8</value>
<value>text/json;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>

</list>
</property>
</bean>

Ⅲ spring mvc4.x怎麼返回json格式

springmvcajax返回json字元串的設置方法:使用技術及環境:Spring3.2.2.RELEASEJackson1.9.10JDK1.6Eclipse3.6Maven3PS:在spring3中,要輸出json數據,只需要添加Jackson 庫到你的classpath。1、項目依賴spring和jackson的依賴:4.0.0com.mkyong.commonSpringMVCwar1.0-.model.Shop;@Controller@RequestMapping("/kfc/brands")publicclassJSONController{@RequestMapping(value="{name}",method=RequestMethod.GET)public@ResponseBodyShopgetShopInJSON(@PathVariableStringname){Shopshop=newShop();shop.setName(name);shop.setStaffName(newString[]{"mkyong1","mkyong2"});returnshop;}}4、mvc:annotation-driven在你的spring配置文件中啟用mvc:annotation-driven註解。5、示例結果

Ⅳ Spring mvc 返回json數組中的日期怎麼格式化

只要繼承它的抽象類:public abstract class JsonSerializer<T>,並在相應的屬性方法上添加指定註解:@JsonSerialize 即可實現。
編寫Date日誌自定義轉換類:
CustomDateSerializer.java
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;

public class CustomDateSerializer extends JsonSerializer<Date> {
@Override
public void serialize(Date value, JsonGenerator jgen,SerializerProvider provider)
throws IOException,JsonProcessingException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = formatter.format(value);
jgen.writeString(formattedDate);
}
}
javabean對應屬性的方法添加註解:
@JsonSerialize(using = CustomDateSerializer.class)
public Date getCreateDate() {
return createDate;
}
就這么簡單就可以實現返回的JSON數據中日期格式自動轉換為:yyyy-MM-dd HH:mm:ss 的格式了。

閱讀全文

與springjson格式化相關的資料

熱點內容
專題學習網站源碼 瀏覽:163
jsphead什麼 瀏覽:88
gps串口數據怎麼發送 瀏覽:968
win10文件主頁共享查看 瀏覽:411
中國聯通有哪些app是免流的 瀏覽:176
邊做邊保存的文件找不到了 瀏覽:858
win10照片應用文件夾名稱 瀏覽:966
編程如何解決資金的原子性 瀏覽:638
如何製作廣角鏡頭矯正文件 瀏覽:513
在網頁開發中應該選用哪個資料庫 瀏覽:742
iphone5移動卡貼 瀏覽:990
電腦文件的格式 瀏覽:127
extjs的xtype 瀏覽:959
suse11iso文件要u盤安裝 瀏覽:153
如何將報表統計數據轉化為圖形 瀏覽:444
如何寄快遞材料文件 瀏覽:265
java構造方法private 瀏覽:475
手機文件找回恢復 瀏覽:516
word怎麼把u盤里的文件拔掉 瀏覽:976
港版蘋果用的插排 瀏覽:1000

友情鏈接