The Bus can carry any event of type RemoteApplicationEvent, but the
default transport is JSON and the deserializer needs to know which
types are going to be used ahead of time. To register a new type it
needs to be in a subpackage of org.springframework.cloud.bus.event.
To customise the event name you can use @JsonTypeName on your custom class
or rely on the default strategy which is to use the simple name of the class.
Note that both the producer and the consumer will need access to the class
definition.
If you cannot or don’t want to use a subpackage of org.springframework.cloud.bus.event
for your custom events, you must specify which packages to scan for events of
type RemoteApplicationEvent using @RemoteApplicationEventScan. Packages
specified with @RemoteApplicationEventScan include subpackages.
For example, if you have a custom event called FooEvent:
package com.acme; public class FooEvent extends RemoteApplicationEvent { ... }
you can register this event with the deserializer in the following way:
package com.acme; @Configuration @RemoteApplicationEventScan public class BusConfiguration { ... }
Without specifying a value, the package of the class where @RemoteApplicationEventScan
is used will be registered. In this example com.acme will be registered using the
package of BusConfiguration.
You can also explicitly specify the packages to scan using the value, basePackages or
basePackageClasses properties on @RemoteApplicationEventScan. For example:
package com.acme; @Configuration //@RemoteApplicationEventScan({"com.acme", "foo.bar"}) //@RemoteApplicationEventScan(basePackages = {"com.acme", "foo.bar", "fizz.buzz"}) @RemoteApplicationEventScan(basePackageClasses = BusConfiguration.class) public class BusConfiguration { ... }
All examples of @RemoteApplicationEventScan above are equivalent,
in that the com.acme package will be registered by explicitly specifying the
packages on @RemoteApplicationEventScan. Note, you can specify multiple base
packages to scan.