对于想了解Jackson2.0与Spring3.1的读者,本文将是一篇不可错过的文章,并且为您提供关于android–使用RoboSpice与Jackson2和Spring、Fastjson、Jack
对于想了解Jackson2.0与Spring 3.1的读者,本文将是一篇不可错过的文章,并且为您提供关于android – 使用RoboSpice与Jackson2和Spring、Fastjson、Jackson与SpringMVC整合的MessageConverter配置、Jackson 2.0 with Spring 3.1、Jackson 与Spring MVC复制嵌套对象不反序列化的有价值信息。
本文目录一览:- Jackson2.0与Spring 3.1
- android – 使用RoboSpice与Jackson2和Spring
- Fastjson、Jackson与SpringMVC整合的MessageConverter配置
- Jackson 2.0 with Spring 3.1
- Jackson 与Spring MVC复制嵌套对象不反序列化
Jackson2.0与Spring 3.1
Spring MVC 3.1与Jackson 2.0兼容吗?Spring
MVC在类路径上自动检测Jackson并委托给Jackson以JSON内容类型的请求是否仍然有效?
答案1
小编典典在Spring 3.2中添加了对Jackson 2的支持,并且已经将其反向移植到Spring
3.1.2(SPR-9507)
android – 使用RoboSpice与Jackson2和Spring
我的libs文件夹包含以下jar.
> commons-io-1.3.2.jar
> commons-lang3-3.2.1.jar
> jackson-annotations-2.2.3.jar
> jackson-core-2.2.3.jar
> jackson-databind-2.2.3.jar
> robospice-1.4.11.jar
> robospice-cache-1.4.11.jar
> robospice-spring-android-1.4.11.jar
> spring-android-core-1.0.1.RELEASE.jar
> spring-android-rest-template-1.0.1.RELEASE.jar
如果写了here,如果找到jackson2 jar,SpringAndroidSpiceService将切换到jackson2.
应用程序因此异常而崩溃:
java.lang.NoClassDefFoundError: org.codehaus.jackson.map.ObjectMapper at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.<init>(MappingJacksonHttpMessageConverter.java:54) at com.octo.android.robospice.JacksonspringAndroidSpiceService.createRestTemplate(JacksonspringAndroidSpiceService.java:33) at com.octo.android.robospice.SpringAndroidSpiceService.onCreate(SpringAndroidSpiceService.java:26) at android.app.ActivityThread.handleCreateService(ActivityThread.java:2572) at android.app.ActivityThread.access$1800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
我错过了什么?
解决方法
Jackson2SpringAndroidSpiceService
而不是JacksonspringAndroidSpiceService. 2件事:)
Fastjson、Jackson与SpringMVC整合的MessageConverter配置
1.Jackson
maven依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.7.1</version>
</dependency>
springmvc-servlet.xml中配置
<mvc:annotation-driven>
<mvc:message-converters>
<bean>
<constructor-arg value="UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
2.FastJson
由于FastJson针对Spring4.2以后进行特殊优化,具体如图
所以FastJson可以分为Spring4.2及以下配置和Spring4.2以上的不同配置
2.1Spring4.2及以下配置
maven依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
springmvc-servlet.xml中配置
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean>
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json</value>
<value>application/xml;charset=UTF-8</value>
</list>
</property>
<property name="features">
<list>
<value>WriteMapNullValue</value>
<value>QuoteFieldNames</value>
<value>WriteDateUseDateFormat</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
2.2Spring4.2以上配置
maven依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
springmvc-servlet.xml中配置
<mvc:annotation-driven validator="validator">
<mvc:message-converters register-defaults="true">
<ref bean="stringHttpMessageConverter"/>
<ref bean="fastJsonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<!--FastJson(spring4.2x版本以上)-->
<bean id="stringHttpMessageConverter">
<constructor-arg value="UTF-8" index="0"></constructor-arg>
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="fastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
<property name="fastJsonConfig">
<bean>
<property name="features">
<list>
<value>AllowArbitraryCommas</value>
<value>AllowUnQuotedFieldNames</value>
<value>DisableCircularReferenceDetect</value>
</list>
</property>
<property name="dateFormat" value="yyyy-MM-dd HH:mm:ss"></property>
</bean>
</property>
</bean>
Jackson 2.0 with Spring 3.1
Spring MVC 3.1与Jackson 2.0兼容吗?Spring MVC在类路径上自动检测Jackson并委托给Jackson以JSON内容类型的请求是否仍然有效?
答案1
小编典典来自spring的Keith Donald前一段时间在Twitter上发布了推文。
Spring MVC对Jackson 2的支持;还可以与Jackson的本机“漂亮打印”功能配合使用https://gist.github.com/2423129
我没有尝试MappingJackson2HttpMessageConverter上面要点中的发现,但是如果不起作用,我会感到惊讶。
Jackson 与Spring MVC复制嵌套对象不反序列化
我正在尝试将以下POJO转换为JSON @RestController
:
@Entity@Table(name="user_location")@NamedQuery(name="UserLocation.findAll", query="SELECT u FROM UserLocation u")public class UserLocation implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String addr1; private String addr2; private String landmark; private BigDecimal lat; private BigDecimal lng; private String zipcode; //bi-directional many-to-one association to City @ManyToOne private City city; //bi-directional many-to-one association to State @ManyToOne private State state; public UserLocation() { } //Getter - Setters}
嵌套的City.java如下:
@Entity@NamedQuery(name="City.findAll", query="SELECT c FROM City c")@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property="@id", scope = City.class)public class City implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; //bi-directional many-to-one association to State @ManyToOne @JsonIgnore private State state; //bi-directional many-to-one association to UserLocation @OneToMany(mappedBy="city") @JsonIgnore private List<UserLocation> userLocations; public City() { } public int getId() { return this.id; } public void setId(int id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } @JsonProperty("state") public State getState() { return this.state; } public void setState(State state) { this.state = state; } public List<UserLocation> getUserLocations() { return this.userLocations; } public void setUserLocations(List<UserLocation> userLocations) { this.userLocations = userLocations; } public UserLocation addUserLocation(UserLocation userLocation) { getUserLocations().add(userLocation); userLocation.setCity(this); return userLocation; } public UserLocation removeUserLocation(UserLocation userLocation) { getUserLocations().remove(userLocation); userLocation.setCity(null); return userLocation; }}
另一个嵌套类State.java如下:
@Entity@NamedQuery(name="State.findAll", query="SELECT s FROM State s")@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property="@id", scope = State.class)public class State implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; //bi-directional many-to-one association to City @OneToMany(mappedBy="state") @JsonIgnore private List<City> cities; //bi-directional many-to-one association to UserLocation @OneToMany(mappedBy="state") @JsonIgnore private List<UserLocation> userLocations; public State() { } public int getId() { return this.id; } public void setId(int id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public List<City> getCities() { return this.cities; } public void setCities(List<City> cities) { this.cities = cities; } public City addCity(City city) { getCities().add(city); city.setState(this); return city; } public City removeCity(City city) { getCities().remove(city); city.setState(null); return city; } public List<UserLocation> getUserLocations() { return this.userLocations; } public void setUserLocations(List<UserLocation> userLocations) { this.userLocations = userLocations; } public UserLocation addUserLocation(UserLocation userLocation) { getUserLocations().add(userLocation); userLocation.setState(this); return userLocation; } public UserLocation removeUserLocation(UserLocation userLocation) { getUserLocations().remove(userLocation); userLocation.setState(null); return userLocation; }}
从UserLocation.java转换的JSON如下:
{ id: 1, addr1: "11905 Technology", addr2: "Eden Prairie", landmark: null, lat: null, lng: null, zipcode: "55344", city: { @id: 1, id: 2, name: "Westborough", state: { @id: 1, id: 2, name: "MA" } }, state: 1}
如您所见,该State
对象作为一个整体进入了内部city
。但是外部的state
(“ UserLocation is showing justan id of
State object. I need to have a same
state object as that of
city
”的属性,而不只是ID。
我对JackSon API比较陌生。请建议我应该采取哪种方法来实现此要求。
谢谢
答案1
小编典典这就是杰克逊设计JsonIdentityInfo批注逻辑的方式。
* Annotation used for indicating that values of annotated type * or property should be serializing so that instances either * contain additional object identifier (in addition actual object * properties), or as a reference that consists of an object id * that refers to a full serialization. In practice this is done * by serializing the first instance as full object and object * identity, and other references to the object as reference values.
Jackson将第一次运行完整的序列化,只有第二次找到该对象时,才会对ID进行序列化。
因此,有两种解决方法:
1)您可以简单地删除@JsonIdentityInfo批注,Jackson将按您期望的方式序列化对象,但将从响应中删除@id字段。这可能很好,因为您仍然会拥有’id’属性。
2)我觉得您可以简单地重组对象并删除一些引用。我会说还是做这些更改是一件好事。首先,您可以从UserLocation中删除对State的引用。我要说的是,因为State附加在City上,所以没有必要将State放在userLocation类中。这样,您将可以从城市访问州,从而解决了您的问题。另外,我将从City类和State类中删除对userLocations列表的引用。
它看起来像:
UserLocation有城市,没有州。
城市有州,没有用户位置
州没有用户位置,也没有城市。
希望这可以帮助
今天关于Jackson2.0与Spring 3.1的讲解已经结束,谢谢您的阅读,如果想了解更多关于android – 使用RoboSpice与Jackson2和Spring、Fastjson、Jackson与SpringMVC整合的MessageConverter配置、Jackson 2.0 with Spring 3.1、Jackson 与Spring MVC复制嵌套对象不反序列化的相关知识,请在本站搜索。
本文标签: