Posts

Spring: How to import data via CSV

In this article, I want to show you how you can import data via a CSV file in a cloud based application. Spring controller In order to upload the CSV file, we will use an HTTP POST request with mime type as multipart/form-data . This request will be processed by my spring rest controller. I will get the file via the request parameter with type :  MultipartFile . If the file content is not empty, I can extract the bytes and encode them as Base64 encoding using java 8 utility. In order to do the work outside of a user-facing request, I use the Google Cloud Platform (GCP) task queues mechanism. I can pass the encoded content as a task parameter. In parallel to the user facing request, a servlet will do the job with the specified parameter. Servlet Consumer The servlet is pretty straightforward : It extracts the encoded content It decode the encoded content It loads object list from the CSV content (jackson csv) It calls a service with the list of objects...

Spring DI in servlet

Today, I'll show you how can inject a bean into your servlet. 1. Using ServletContextListener The ServletContextListener will enable your servlets to access the Spring ApplicationContext . In your servlet : 2. Using the SpringBeanAutowiringSupport I personally prefer the following solution : thanks for sharing...

Spring JPA : Using Specification with Projection

You may probably know that using Specification with Projection is not possible at this moment. I'll show you how you can easily bypass this problem using the JPA Criteria. 1. Create an extended repository We begin by creating an extending global repository as follow : You can find more information here . 2. Add our custom method In order to limit the fields returned for each item, the consumer of my API must provide the required fields this way: /orders?fields=productId,name,quantity . ( Best Practices ) Basically, I'm defining the SELECT clause with the returned fields (cf. getSelections() ): 😎Happy coding...

Spring JPA : test interface-based projection using factory

In this short article, I'll show you how you can easily test a repository method which uses interface-based projection . Our repository: You can use the above method this ways: I use the ProjectionFactory as well as the SpelAwareProxyProjectionFactory for testing ( spring doc ). You must also provide the setters on your interface as follow: Happy reading...

JPA : How to deal with one to many association

After reading and experiencing lot of solutions for modeling a one to many association, I'll show you the ways you can REALLY do it: 1. Using bidirectional association the parent entity : As you can see, I use the @oneToMany annotation with the following properties : mappedBy --> parent property of the child cascade --> ALL by default orphanRemoval --> true by default fetch --> we use  LAZY for performance reason I use a Set for the collection. I provide also a helper method for adding a child to the parent (You can do the same for removing a child). The child entity below implements also the equals and hashCode methods: In order to retrieve the parent and the children with ONE query, we have 2 solutions: @NamedEntityGraph : this annotation tell JPA which node to load automatically.  @NamedQuery using LEFT JOIN FETCH These methods are both used in the below repository: 2. @ManyToOne ONLY in the child entity  ...

Angular 7: A template to be up to speed in no time

Today, I'll share with you an Angular 7 application from where you can start building your own. I've followed as much as possible the best practices available here . You can find the source code at this repository . If you can improve this solution, do not hesitate to tell me. 😀 Let's inspect the different parts: Core Module 1. guards I've incorporated basically 2 guards:  The AuthGuard implements both CanActivate and CanLoad . This guard is responsible for checking if the current user is authenticated and authorized. If not authenticated, the user will be redirected to the login page. If the authenticated user hasn't sufficient permission, he will be redirected to the home page. The NoAuthGuard which implements CanActivate , provides a way to restrict a route when a user is already logged in (e.g. login). 2. interceptors The ErrorInterceptor basically intercepts client errors and server side errors. When it catches a 401 or 403 ...

Angular 7 | RxJS : Test finalize()

In the following interceptor, I want to test if the method loaderService.stopLoading() is properly called : As you can see this function is called from the finalize() RxJS operator. The unit test for this interceptor is as follow: Thanks for sharing...