Getting Started - lvyahui8/spring-boot-data-aggregator GitHub Wiki
<dependency>
<groupId>io.github.lvyahui8</groupId>
<artifactId>spring-boot-data-aggregator-starter</artifactId>
<version>${latestVersion}</version>
</dependency>
application.properties
# Specify the package to scan the annotations
io.github.lvyahui8.spring.base-packages=io.github.lvyahui8.spring.example
Developing a user summary data interface that includes the user's basic information and blog list.
Use @DataProvider
to define the interface a data provider.
Use @InvokeParameter
to specify the input parameters to pass.
Blog list service
require input parameter userId
.
@Service
public class PostServiceImpl implements PostService {
@DataProvider("posts")
@Override
public List<Post> getPosts(@InvokeParameter("userId") Long userId) {
User basic information query service
require input parameter userId
.
@Service
public class UserServiceImpl implements UserService {
@DataProvider("user")
@Override
public User get(@InvokeParameter("userId") Long id) {
@Autowired
DataBeanAggregateQueryFacade dataBeanAggregateQueryFacade;
User user = dataBeanAggregateQueryFacade.get(
Collections.singletonMap("userId", 1L),
new Function2<User, List<Post>, User>() {
@Override
public User apply(@DataConsumer("user") User user,
@DataConsumer("posts") List<Post> posts) {
user.setPosts(posts);
return user;
}
});
Assert.notNull(user,"user not null");
Assert.notNull(user.getPosts(),"user posts not null");
Combine @DataProvider
( @DataConsumer
\ @InvokeParameter
) to achieve aggregation function
@Component
public class UserAggregate {
@DataProvider("userWithPosts")
public User userWithPosts(
@DataConsumer("user") User user,
@DataConsumer("posts") List<Post> posts) {
user.setPosts(posts);
return user;
}
}
Specify queried data id, invoke parameters, and return type to invoke facade.get
method
DataBeanAggregateQueryFacade queryFacade = context.getBean(DataBeanAggregateQueryFacade.class);
User user = queryFacade.get(/*data id*/ "userWithPosts",
/*Invoke Parameters*/
Collections.singletonMap("userId",1L),
User.class);
Assert.notNull(user,"user not null");
Assert.notNull(user.getPosts(),"user posts not null");