我在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.class
@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
注释,但这并不是我工作的差异。所以我问你,我做错了什么,它不能正确序列化?
如果描述不够,抱歉,我不知道还能写些什么来解决这个问题。
@Edit 我正在使用ResponseEntity类
返回响应beanreturn new ResponseEntity<ResponseWrapper>(response, HttpStatus.OK);
答案 0 :(得分:0)
我想在这里,杰克逊正在尝试序列化一个空对象。 你需要配置jackson以避免这件事:
jackson 1.x:
myObjectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
jackson 2.X
myObjectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
可以在here
找到在Spring中执行此操作的示例