Java springbootbean反序列化问题

Java springbootbean反序列化问题,java,spring,spring-boot,Java,Spring,Spring Boot,我无法理解以下问题。我有几个bean,它们是SessionScoped @Configuration @Profile({ "production", "integration" }) public class CloudConfiguration { private static final Logger LOGGER = LoggerFactory.getLogger(CloudConfiguration.class); @Bean @SessionScope public Connect

我无法理解以下问题。我有几个bean,它们是
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() {...
现在,这些Bean通过构造函数被注入到另一个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
    }
我得到以下反序列化错误

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的属性为Null(因此出现错误),但我不知道为什么,因为我还可以看到构造函数是正确运行的。 我也尝试过使用
@postcontract
来达到同样的目的

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


向Mathias问好阅读信息;它确切地告诉你你做错了什么


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

Hi!我无法理解通过构造函数将
SessionScope
d bean注入另一个bean的位置。我没有在
TenantInfo
类的构造函数上看到任何
@autowired
注释。如果你有一个构造函数,你不需要@autowired,我相信你,但我不明白其中的部分是没有意义的。我可以通过
new TenantInfo(..)
返回POJO,没有问题,它们被反序列化,还有BeanDeserializer,我不知道bean代理不能被反序列化。那么解决办法就是编写一个定制的MessageConverter?@MathiasMaerker谢谢,但我所说的是事实,事实不需要信任投票。SpringBean用于依赖注入——如果您的目的是返回
tenantId
tenantAccount
,那么您需要创建一个POJO,或者Spring所称的模型。