找不到类 java.util.logging.ErrorManager 的序列化程序,也没有发现用于创建 BeanSerializer 的属性

2024-01-02

我在 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"])

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

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

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

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

我认为在这里,杰克逊正在尝试序列化一个空对象。 你需要配置 jackson 来避免这种情况:

杰克逊 1.x:

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

杰克逊2.X

myObjectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

可以找到在 Spring 中执行此操作的示例here https://stackoverflow.com/questions/28862483/spring-and-jackson-how-to-disable-fail-on-empty-beans-through-responsebody

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

找不到类 java.util.logging.ErrorManager 的序列化程序,也没有发现用于创建 BeanSerializer 的属性 的相关文章

随机推荐