【问题标题】:No serializer found for class java.util.logging.ErrorManager and no properties discovered to create BeanSerializer没有为类 java.util.logging.ErrorManager 找到序列化程序,也没有找到用于创建 BeanSerializer 的属性
【发布时间】:2017-05-27 07:45:39
【问题描述】:

我在 REST 应用程序中序列化响应时遇到问题。

这是我的代码的快速快照: ResponseWrapper.class

@JsonInclude(Include.NON_NULL) 
public class ResponseWrapper {

     private User user;
     private Token token;
     private Authentication authentication;

     public ResponseWrapper(){}
     //setters and getters
}

配置类

@Configuration
public class BeanConfig {

@Bean
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode =    ScopedProxyMode.TARGET_CLASS) 
public ResponseWrapper response() {
    return new ResponseWrapper();
}   

}

在我的实现类中,我得到了一个自动装配的变量:

@Autowired
ResponseWrapper response;

当我返回我的回复就像

return response;

我收到了一条消息

Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: No serializer found for class java.util.logging.ErrorManager and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.coig.prek.webservice.utils.wrappers.ResponseWrapper$$EnhancerBySpringCGLIB$$9e771672["targetSource"]->org.springframework.aop.target.SimpleBeanTargetSource["beanFactory"]->org.springframework.beans.factory.support.DefaultListableBeanFactory["beanClassLoader"]->org.apache.catalina.loader.ParallelWebappClassLoader["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["parent"]->java.util.logging.Logger["parent"]->java.util.logging.Logger["handlers"]->org.apache.juli.AsyncFileHandler["errorManager"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.util.logging.ErrorManager and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.coig.prek.webservice.utils.wrappers.ResponseWrapper$$EnhancerBySpringCGLIB$$9e771672["targetSource"]->org.springframework.aop.target.SimpleBeanTargetSource["beanFactory"]->org.springframework.beans.factory.support.DefaultListableBeanFactory["beanClassLoader"]->org.apache.catalina.loader.ParallelWebappClassLoader["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["parent"]->java.util.logging.Logger["parent"]->java.util.logging.Logger["handlers"]->org.apache.juli.AsyncFileHandler["errorManager"])

而且我实际上不知道我做错了什么。我尝试用@JsonProperty 注释@JsonIgnore,但我工作并没有什么不同。所以我问你,我做错了什么,它不能正确序列化?

如果描述不够,对不起,我不知道我还能写什么来解决这个问题。

@编辑 我正在使用 ResponseEntity 类返回响应 bean

return new ResponseEntity<ResponseWrapper>(response, HttpStatus.OK);

【问题讨论】:

    标签: java json rest serialization spring-boot


    【解决方案1】:

    我认为在这里,杰克逊试图序列化一个空对象。 你需要配置jackson来避免这件事:

    杰克逊 1.x:

    myObjectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
    

    杰克逊 2.X

    myObjectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    

    在 Spring 中做这件事的一个例子可以找到 here

    【讨论】:

    • 问题是,当我这样做时,由于某种原因,我得到了“无限递归”。另一方面,序列化对象永远不会为空。
    • 尝试从响应实例中删除Autowired
    • 但如果我理解正确,我将无法调用该 bean 的实例
    • 我认为响应应该由代码而不是容器创建。所以删除它应该是好的,
    猜你喜欢
    • 2014-11-21
    • 1970-01-01
    • 2020-04-22
    • 1970-01-01
    • 2012-10-25
    • 2018-02-05
    • 2016-02-17
    • 2021-06-26
    • 2014-09-19
    相关资源
    最近更新 更多