Java Jackson 2和Spring自动连线bean

Java Jackson 2和Spring自动连线bean,java,json,spring,spring-mvc,jackson,Java,Json,Spring,Spring Mvc,Jackson,我的用户对象的Jackson序列化遇到问题。 有几个带有getter和setter的私有字段。当我这样做时,一切都很好: public String json() { MyUser user = new MyUser(); user.setUsername("myName"); return mapper.writeValueAsString(user); // Valid JSON } 但我想用Spring Framework自动连接用户对象: @Autowired pr

我的用户对象的Jackson序列化遇到问题。 有几个带有getter和setter的私有字段。当我这样做时,一切都很好:

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

   return mapper.writeValueAsString(user); // Valid JSON
}
但我想用Spring Framework自动连接用户对象:

@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实例做了一些错误,因此Jackson无法序列化它

有办法解决吗

更新

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方法。

您的用户是否包含引用该用户的字段?请共享您的
MyUser
类我已经用MyUser类更新了我的问题。谢谢。但是,如果我的用户类中有20个字段呢?这些字段以后可能会被修改。还有其他方法可以序列化自动连线POJO吗?@silent box可能还有其他方法-但我还没有遇到这种情况,所以我不能确定。可能有一些方法会告诉映射器将传递的对象视为超类,即
MyUser
,或者一些具有类似含义的
@Json…
注释。ª洎。СПазззз:)@silent boxзззгзззгзгзззз)我认为从他的描述。