[英]Jackson 2 and Spring Autowired bean
我遇到了我的User對象的Jackson序列化問題。 有一些私人領域有getter和setter。 當我做這樣的事情時,一切正常:
public String json() {
MyUser user = new MyUser();
user.setUsername("myName");
return mapper.writeValueAsString(user); // Valid JSON
}
但是我想用Spring Framework自動裝配User對象:
@Autowired
private MyUser user;
public String json() {
System.out.println(user.getUsername()); // Property set before and it works
return mapper.writeValueAsString(user); // Error
}
這不起作用。 我有一個錯誤:
com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.apache.juli.OneLineFormatter and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.piggymetrics.classes.PiggyUser$$EnhancerBySpringCGLIB$$5f23855e["targetSource"]->org.springframework.aop.target.SimpleBeanTargetSource["beanFactory"]->org.springframework.beans.factory.support.DefaultListableBeanFactory["parentBeanFactory"]->org.springframework.beans.factory.support.DefaultListableBeanFactory["beanClassLoader"]->org.apache.catalina.loader.WebappClassLoader["resources"]->org.apache.catalina.webresources.StandardRoot["context"]->org.apache.catalina.core.StandardContext["logger"]->org.apache.juli.logging.DirectJDKLog["logger"]->java.util.logging.Logger["parent"]->java.util.logging.Logger["handlers"]->org.apache.juli.AsyncFileHandler["formatter"])
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:59)
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:26)
當我試圖忽略這些未知錯誤時
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
我得到了無限遞歸:
com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: org.apache.catalina.core.StandardEngineValve["container"]->org.apache.catalina.core.StandardEngine["pipeline"]->org.apache.catalina.core.StandardPipeline["basic"]->org.apache.catalina.core.StandardEngineValve["container"]
...
看起來Spring在自動裝配的MyUser實例上做錯了,所以傑克遜無法將其序列化。
有辦法解決嗎?
UPDATE
MyUser類非常簡單:
package com.metrics.classes;
import com.metrics.classes.interfaces.User;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;
@Component
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class MyUser implements User {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
由於您使用MyUser
作為Spring托管bean,Spring將您的對象包裝到代理中 - 所以當您調用mapper.writeValueAsString(user);
你實際上是通過代理作為參數。 所述代理包含一些屬性,即映射器無法序列化。
您可以嘗試在序列化之前應用過濾器以僅包含所需的屬性:
ObjectMapper mapper = new ObjectMapper();
SimpleFilterProvider simpleFilterProvider = new SimpleFilterProvider()
.addFilter("myUser", simpleBeanPropertyFilter.filterOutAllExcept("username"));
mapper.setFilters(filterProvider);
return mapper.writeValueAsString(user);
UnknownSerializer
類中拋出此異常。 以下是引發異常的確切代碼:
throw new JsonMappingException("No serializer found for class "+value.getClass().getName()
+" and no properties discovered to create BeanSerializer
(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )");
該錯誤消息附加了無法序列化的類的名稱。 在您的情況下,基於錯誤消息問題是org.apache.juli.OneLineFormatter
類。 因此, MyUser
類沒有問題。
現在出現這個錯誤的原因,在序列化一個私有所有字段的實體時拋出它。 在org.apache.juli.OneLineFormatter
類中,所有字段都是私有的,沒有任何公共getter和setter方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.