【问题标题】:Instantiating a list of object in a class of a module from within other modules从其他模块中实例化模块类中的对象列表
【发布时间】:2022-08-22 14:48:55
【问题描述】:

我在使用 Axon 框架的 Spring Boot 微服务项目中有两个模块。在定义为保留模型的核心项目的第一个模块中,我有两个类(用户和角色),其中用户类包含角色列表:

////// The User and Role classes are in the first module
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Integer id;
    private String username;
    private String password;
    private String email;
    private boolean enabled = false;
    private boolean accountNonExpired = true;
    private boolean credentialsNonExpired = true;
    private boolean accountNonLocked = true;
    private List<Role> roles;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Role {
    private Integer id;
    private String name;
}

在第二个模块中,我在 POM 文件中添加了第一个模块的依赖项,以便访问 User 和 Role 类:

<dependency>
  <groupId>com.main</groupId>
  <artifactId>core</artifactId>
  <version>0.0.1</version>
</dependency>

在第二个模块中,当我尝试创建用户对象时,它会出错并且不会将用户保存到事件存储并抱怨角色类,特别是角色列表。以下是第二个模块中的控制器类,它正在创建要发送到 Axon 的用户对象:

@RestController
@RequestMapping(path = \"/api/v1/registerUser\")
public class RegisterUserController {

private final CommandGateway commandGateway;
......

@PostMapping
public ResponseEntity<?> registerUser(@RequestBody User user)
{
  Role role = new Role();
  role.setId(1);
  role.setName(\"Admin\");
  
  List<Role> roles = new ArrayList<Role>();
  roles.add(role);

  ....

  user.setRole(roles);

  RegisterUserCommand command = new RegisterUserCommand();
  command.setId(id);
  command.setUser(user);

  commandGateway.sendAndWait(command); /// Send the user to Axon

  ....  
}

重要的:当我尝试只使用一个模块并将用户和角色类、控制器和所有东西放在该模块中时,它没有问题。

堆栈塔斯:

2022-08-16 12:06:44.463  INFO 29816 --- [           main] c.h.user.cmd.api.UserCommandApplication  : Starting UserCommandApplication using Java 17.0.4 on DESKTOP-JUNLICE with PID 29816 (E:\\Hawramgard_main\\User_Management\\user.cmd.api\\target\\classes started by Venos46131105 in E:\\Hawramgard_main\\User_Management)
2022-08-16 12:06:44.465  INFO 29816 --- [           main] c.h.user.cmd.api.UserCommandApplication  : No active profile set, falling back to 1 default profile: \"default\"
2022-08-16 12:06:45.249  INFO 29816 --- [           main] o.s.c.a.ConfigurationClassPostProcessor  : Cannot enhance @Configuration bean definition \'org.axonframework.springboot.autoconfig.EventProcessingAutoConfiguration\' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as \'static\'.
2022-08-16 12:06:45.360  INFO 29816 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean \'(inner bean)#4a1e3ac1#31\' of type [org.axonframework.spring.config.annotation.SpringContextParameterResolverFactoryBuilder$ClasspathParameterResolverFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-08-16 12:06:45.361  INFO 29816 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean \'(inner bean)#4a1e3ac1#31\' of type [org.axonframework.messaging.annotation.MultiParameterResolverFactory] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-08-16 12:06:45.364  INFO 29816 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean \'(inner bean)#4a31c2ee#31\' of type [org.axonframework.spring.config.annotation.SpringBeanDependencyResolverFactory] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-08-16 12:06:45.364  INFO 29816 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean \'(inner bean)#7be71476\' of type [org.axonframework.spring.config.annotation.SpringBeanParameterResolverFactory] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-08-16 12:06:45.366  INFO 29816 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean \'__axon-parameter-resolver-factory\' of type [org.axonframework.spring.config.ApplicationContextLookupParameterResolverFactory] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-08-16 12:06:45.366  INFO 29816 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean \'__axon-parameter-resolver-factory\' of type [org.axonframework.messaging.annotation.MultiParameterResolverFactory] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-08-16 12:06:45.374  INFO 29816 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean \'(inner bean)#7b7683d4\' of type [org.axonframework.spring.config.annotation.SpringHandlerDefinitionBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-08-16 12:06:45.374  INFO 29816 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean \'(inner bean)#7b7683d4\' of type [org.axonframework.messaging.annotation.MultiHandlerDefinition] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-08-16 12:06:45.375  INFO 29816 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean \'(inner bean)#3efedc6f\' of type [org.axonframework.spring.config.annotation.SpringHandlerEnhancerDefinitionBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-08-16 12:06:45.375  INFO 29816 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean \'(inner bean)#3efedc6f\' of type [org.axonframework.messaging.annotation.MultiHandlerEnhancerDefinition] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-08-16 12:06:45.376  INFO 29816 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean \'__axon-handler-definition\' of type [org.axonframework.messaging.annotation.MultiHandlerDefinition] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-08-16 12:06:45.653  INFO 29816 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2022-08-16 12:06:45.667  INFO 29816 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-08-16 12:06:45.667  INFO 29816 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-08-16 12:06:45.769  INFO 29816 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-08-16 12:06:45.769  INFO 29816 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1265 ms
2022-08-16 12:06:45.977  INFO 29816 --- [           main] o.a.s.a.XStreamAutoConfiguration         : Initializing an XStream instance since none was found. The auto configuration base packages will be used as wildcards for the XStream security settings.
2022-08-16 12:06:46.083  INFO 29816 --- [           main] o.a.s.util.XStreamSecurityTypeUtility    : Constructing wildcard type for base package [com.hawramgard.user.cmd.api].
2022-08-16 12:06:46.091  INFO 29816 --- [           main] o.a.serialization.ChainingConverter      : ContentTypeConverter of type [class org.axonframework.serialization.xml.Dom4JToByteArrayConverter] is ignored. It seems to rely on a class that is not available in the class loader: org/dom4j/Document
2022-08-16 12:06:46.091  INFO 29816 --- [           main] o.a.serialization.ChainingConverter      : ContentTypeConverter of type [class org.axonframework.serialization.xml.InputStreamToDom4jConverter] is ignored. It seems to rely on a class that is not available in the class loader: org/dom4j/Document
2022-08-16 12:06:46.092  INFO 29816 --- [           main] o.a.serialization.ChainingConverter      : ContentTypeConverter of type [class org.axonframework.serialization.xml.XomToStringConverter] is ignored. It seems to rely on a class that is not available in the class loader: nu/xom/Document
2022-08-16 12:06:46.092  INFO 29816 --- [           main] o.a.serialization.ChainingConverter      : ContentTypeConverter of type [class org.axonframework.serialization.xml.InputStreamToXomConverter] is ignored. It seems to rely on a class that is not available in the class loader: nu/xom/ParsingException
2022-08-16 12:06:46.145  WARN 29816 --- [           main] .s.s.UserDetailsServiceAutoConfiguration : 


2022-08-16 12:06:46.309  INFO 29816 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Will secure any request with [org.springframework.security.web.session.DisableEncodeUrlFilter@1ee5632d, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@52a7928a, org.springframework.security.web.context.SecurityContextPersistenceFilter@2f9dda71, org.springframework.security.web.header.HeaderWriterFilter@5fffb692, org.springframework.web.filter.CorsFilter@563ccd31, org.springframework.security.web.authentication.logout.LogoutFilter@739831a4, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@39023dbf, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@7f5fcfe9, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@67688110, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@72465eb2, org.springframework.security.web.session.SessionManagementFilter@6282b9f5, org.springframework.security.web.access.ExceptionTranslationFilter@79d7035, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@2bab618]
2022-08-16 12:06:46.730  INFO 29816 --- [           main] i.a.a.c.impl.AxonServerManagedChannel    : Requesting connection details from localhost:8124
2022-08-16 12:06:47.147  INFO 29816 --- [           main] i.a.a.c.impl.AxonServerManagedChannel    : Successfully connected to localhost:8124
2022-08-16 12:06:47.158  INFO 29816 --- [           main] i.a.a.connector.impl.ControlChannelImpl  : Connected instruction stream for context \'default\'. Sending client identification
2022-08-16 12:06:47.170  INFO 29816 --- [           main] i.a.a.c.command.impl.CommandChannelImpl  : CommandChannel for context \'default\' connected, 0 command handlers registered
2022-08-16 12:06:47.174  INFO 29816 --- [           main] i.a.a.c.command.impl.CommandChannelImpl  : Registered handler for command \'com.hawramgard.user.cmd.api.commands.RegisterUserCommand\' in context \'default\'
2022-08-16 12:06:47.179  INFO 29816 --- [           main] i.a.a.c.command.impl.CommandChannelImpl  : Registered handler for command \'com.hawramgard.user.cmd.api.commands.RemoveUserCommand\' in context \'default\'
2022-08-16 12:06:47.180  INFO 29816 --- [           main] i.a.a.c.command.impl.CommandChannelImpl  : Registered handler for command \'com.hawramgard.user.cmd.api.commands.UpdateUserCommand\' in context \'default\'
2022-08-16 12:06:47.200  INFO 29816 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path \'\'
2022-08-16 12:06:47.215  INFO 29816 --- [           main] c.h.user.cmd.api.UserCommandApplication  : Started UserCommandApplication in 3.056 seconds (JVM running for 5.815)
2022-08-16 12:07:13.096  INFO 29816 --- [nio-8081-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet \'dispatcherServlet\'
2022-08-16 12:07:13.096  INFO 29816 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet \'dispatcherServlet\'
2022-08-16 12:07:13.096  INFO 29816 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms
RegisterUserCommand(id=1638, user=com.hawramgard.user.core.models.User@7738b41d)
org.axonframework.commandhandling.CommandExecutionException: com.hawramgard.user.core.models.Role
  • 我需要提一下,我在用户类中将private List&lt;Role&gt; roles 更改为private Role role,并且工作正常。但是在对象列表的情况下,它有问题。
  • 你好@Pezhman!您能与我分享确切的错误和堆栈跟踪吗?这将帮助我查明问题。此外,您是否也在使用 Axon Server?
  • 谢谢@Mitchell Herrijgers 的评论。是的,我通过使用 Docker 来使用 Axon 服务器。
  • 不幸的是,我看不到有关错误的详细信息。但是,我注意到您使用的是 Java17 和 XStream,对吗? Java 17 上的 XStream 有很多问题。您可以尝试使用早期的 Java 版本,还是使用JacksonSerializer
  • 这是一个例外:org.axonframework.commandhandling.CommandExecutionException: com.hawramgard.user.core.models.Role。我没有使用 XStream。 Java 版本是 17。好吧,我会尝试 Java 和 JacksonSerializer 的早期版本。

标签: java spring-boot microservices axon


【解决方案1】:

当 Jackson 需要序列化一个列表时,它会丢失类型信息并且无法反序列化它。这可以在 Axon Framework 中使用以下配置解决:

@Configuration
public class SerializerConfig {
    @Bean
    public Serializer jacksonSerializer(ObjectMapper objectMapper) {
        return JacksonSerializer.builder()
                .defaultTyping()
                .build();
    }
}

请注意,这也可以使用@JsonTypeInfo 注释来解决。有关详细信息,请参阅 JacksonSerializer 上的 defaultTyping() 方法的 javadoc:

将底层 ObjectMapper 配置为在将 Java 对象序列化为 JSON 时包含类型信息。具体来说,它使用 ObjectMapper.DefaultTyping.NON_CONCRETE_AND_ARRAYS 调用 ObjectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping) 方法。这可以打开以允许对象的 java.util.Collections(例如查询 java.util.List 响应)自动包含类型,而无需在对象本身上使用 com.fasterxml.jackson.annotation.JsonTypeInfo。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    相关资源
    最近更新 更多