Ⅰ 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 的格式了。