Custom business logic with SpringBot
One benefit of the Codebots platform is that developers can take advantage of bot-written code to reduce boilerplate and redundant work, while increasing productivity and customisability with protected regions.
This article will walk you through some of the most common files which you will interact with when customising your application. In particular, this article will summarise:
- Repositories
- Services
- Query resolvers
Repositories
Repositories are data layer that sit on top of the underlying database. In SpringBot, this layer makes use of Hibernate which in turn elevates Java Persistence API, or JPA.
A repository is simply a Java interface that is annotated with @Repository
, which registers itself to Spring upon bootstrapping.
For example, a repository may be as simple as
@Repository
public class FishRepository extends PagingAndSortingRepository<FishEntity, UUID> {
List<FishEntity> findAll();
List<FishEntity> findByName(String name);
}
This is a repository that fetches all students, or students with a given name. For more details on repositories, check out the Spring Repositories documentation.
Here we can extend FishRepository
with PagingAndSortingRepository
. When you add more methods, Spring will automatically generate an implementation class with those methods. PagingAndSortingRepository
requires an entity class and the ID type of that entity class. Refer to the
PagingAndSortingRepository documentation for more details.
In SpringBot, all repository classes extend AbstractRepository.java
which itself extends PagingAndSortingRepository
.
If you require more direct interaction with the database, custom SQL files can be added for more fine grain control. See Custom SQL with SpringBot for more details.
Services
Although repositories are suitable for raw interaction with the underlying database, they are not suitable for more complex logic that are more specific to the application domain. Instead, this logic should be implemented in services.
Services are a layer that sits on top of the data layer, composed of complex business logic. In SpringBot, they can be found in the serverside/src/main/java/project/services
directory.
Here you can define custom services or methods for existing services to suit your domain or problem at hand. Each method can make use of multiple repositories or other services to solve a particular problem.
Building on the previous repository example, let’s say we want a service to fetch all the students whose name start with a given character.
Our simple service will look like this:
@Service
public class FishService {
@Autowired
private FishRepository fishRepository;
public List<String> getFishWhoseNameStartsWith(String prefix) {
return fishRepository.findAll()
.filter(fish -> fish.getName().startsWith(prefix))
.collect(Collectors.toList());
}
}
This example has been highly simplified for demonstration purposes. All services in SpringBot are guarded against Spring Security which derives its security rule from the Security Diagram. Refer to Security handling with SpringBot server-side for more details. It is also not optimised for tables with large records. Instead, you will need to custom SQL queries to optimise your runtime. See Custom SQL with SpringBot for more information.
By default, SpringBot comes with many default service methods that you can elevate in your code. If you want to add custom code to have better control over your logic, refer to the Custom REST API endpoint with SpringBot and Custom Search Queries with SpringBot articles for more details.
Resolvers
Resolvers sits on top of services to provide external GraphQL definitions. A GraphQL query or mutation may elevate multiple services to compose or process high-level logic before returning them to the client-side. This is unlike services where some may be internal to the server-side only.
Resolvers can be located at serverside/src/main/java/{projectDomain}/graphql/resolvers
. All resolvers in SpringBot were built upon GraphQL Java Tools and GraphQL Java Spring to provide better integration with Spring.
For example, here is a simple resolver:
@Component
public class FishResolver implements GraphQLQueryResolver {
@Autowired
private FishService fishService;
public List<FishEntity> fishWhoseNameStartsWith(String name) {
return fishService.getFishWhoseNameStartsWith(name);
}
}
Here we simply create a new query method to expose our service method to external parties.
For more information on how to create your own API endpoint both for GraphQL and RESTful, refer to Custom REST API endpoint with SpringBot.
… and more
SpringBot has been designed to offer you maximum customisability. This article has briefly touched some of the most common functionality of the application, but every source file has been designed with protected regions.
Was this article helpful?