Posts

Showing posts from July, 2019

Spring Service method validation

It is considered a good practice to validate the inputs of your service method. You need to first annotate your service class with @Validated . This annotation tells Spring to evaluate the constraints annotations on each method parameters. In order to avoid a ConstraintDeclarationException , your interface should also declare the constraint annotations. If the input is invalid, a ConstraintViolationException is thrown.

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...