2015-03-31 93 views
4

我遇到了Jackson序列化User Box对象的问题。 有一些私人领域与getter和setters。一切工作正常,当我做这样的事情:Jackson 2和Spring Autowired bean

public String json() { 
    MyUser user = new MyUser(); 
    user.setUsername("myName"); 

    return mapper.writeValueAsString(user); // Valid JSON 
} 

但我想自动装配用户对象与Spring框架:

@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"] 
... 

看起来像春天做错了什么用自动装配的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; 
    } 
} 
+0

您的用户是否包含对用户有引用的字段? – JEY 2015-03-31 15:03:33

+0

请分享你的MyUser类 – Mithun 2015-03-31 15:10:07

+0

我已经更新了我的问题whit MyUser类。谢谢。 – 2015-03-31 15:22:19

回答

2

由于您使用MyUser作为一个Spring管理的bean,春包装你的对象为代理 - 所以,当你调用mapper.writeValueAsString(user);你实际上是在传递作为参数的代理。该代理包含一些属性,该映射器无法序列化。

您可以尝试序列化之前将过滤器只包含您需要的属性:

ObjectMapper mapper = new ObjectMapper(); 
SimpleFilterProvider simpleFilterProvider = new SimpleFilterProvider() 
    .addFilter("myUser", simpleBeanPropertyFilter.filterOutAllExcept("username")); 

mapper.setFilters(filterProvider); 
return mapper.writeValueAsString(user); 
+0

是的,它的工作原理。谢谢。 – 2015-03-31 16:04:09

+0

但是,如果我在MyUser类中有20个字段呢?而且这些字段可能稍后会修改。是否有另一种序列化Autowired POJO的方法? – 2015-03-31 16:10:19

+0

@ silent-box可能有其他方法 - 但我还没有遇到过这种情况,所以我不能确定。可能有一些方法可以告诉映射器将对象作为超类来处理 - 即'MyUser'或者一些具有相似含义的@Json ...'注释。 – Vladimir 2015-03-31 16:15:56

1

此例外是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方法。

+0

我认为从他的描述中可以清楚地看到他的领域有公开的获得者和接球手。 – user2076066 2016-01-14 09:37:14