Spring boot bean反序列化问题

时间:2018-04-06 07:15:54

标签: java spring spring-boot

我有一个问题,了解以下问题。我有几个豆SessionScoped

@Configuration
@Profile({ "production", "integration" })
public class CloudConfiguration {
private static final Logger LOGGER = LoggerFactory.getLogger(CloudConfiguration.class);

@Bean
@SessionScope
public ConnectivityConfiguration getTenantConfiguration() {...

    @Bean
@SessionScope
public ConnectivityConfiguration getDestinationConfiguration() {...

现在这些Beans通过构造函数注入另一个Bean

@Service
@SessionScope
public class TenantInfo {
    private static final Logger LOGGER = LoggerFactory.getLogger(TenantInfo.class);

    private String tenantId;
    private String tenantAccount;


    public TenantInfo(TenantContext context) {
        LOGGER.debug("Creating TenantInfo");
        this.tenantId = context.getTenant().getId();
        this.tenantAccount = context.getTenant().getAccount().getId();

现在我在Rest服务中返回TenantInfo Bean

@Autowired
TenantInfo tenantInfo
    @GetMapping(path = "/tenantInfo")
    public TenantInfo getTenantInfo(HttpServletResponse response) throws RestClientException {
return tenantInfo
    }

我收到以下deserializatíon错误

Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No serializer found for class org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.tts.scp.converter.controllers.TenantInfo$$EnhancerBySpringCGLIB$$c90fa84a["CGLIB$CALLBACK_0"])

我可以在调试器中看到TenantInfo的属性是空的(因此是错误的),但我不明白为什么,因为我也可以看到Contructor正确运行。 我也尝试使用@PostContruct到同一目的。

所以我的问题是:这到底发生了什么?为什么Bean看似正确创建,但之后所有属性都为空?

关心Mathias

1 个答案:

答案 0 :(得分:1)

阅读邮件;它告诉你你到底做错了什么。

当您从RestController中的处理程序方法返回一个对象时,Spring会尝试根据内容协商找到可以序列化该对象的HttpMessageConverter。从方法返回Spring bean是没有意义的;没有HttpMessageConverter可以接受bean代理并在线路上对其进行序列化。