@PreAuthorize Annotation – hasRole Example In Spring Security

Spring Security Has-Role In this tutorial I will show you an example on @PreAuthorize annotation – hasRole() example in Spring Security. @PreAuthorize is the most useful annotation that decides whether a method can actually be invoked or not based on user’s role. hasRole() method returns true if the current principal has the specified role. By default if the supplied role…

Spring Service Layer Bean Validation

Introduction In my previous tutorial I have shown how to validate input data coming from client or end user to Spring REST Controller. Now in this tutorial I will show you how to validate data in Spring Service Layer. The reason behind validating input data in Service Layer is, input data from client may not always pass through the REST…

Conditional Statement in Spring Config

Sometimes we may need to have some conditional statement in Spring config XML file depending upon some variables value. Think of a situation when you are using JPA in your application and your application may have two persistence units in Spring XML config file and you need to connect to one of them depending upon the environments. Here conditional statement…

Spring REST Optional Path Variable Example

Optional Path Variable In this tutorial I will show you how to work with Spring framework’s optional path variable (PathVariable) in REST services. Here I will use Java 8’s java.util.Optional or using attribute required=false and at least Spring Boot 2.1.4 to support optional PathVariable. The optional variable can be found in the URI resource path of the URL.

Validate input data in REST service

Introduction In this tutorial I will show you how you can validate your input data while working with REST service. Generally you need to validate the input data coming from end users because users may give anything as an input but your service side code should not accept anything as an input for the defined field. Prerequisites Please go through…

Documenting Spring REST API with Swagger 2

When creating a REST API, good documentation is instrumental. But developers do not have any option using JSR-339 to generate any documentation on all these APIs so other developers could quickly understand how to use them. For sure, it would be just awesome to generate verbose and easy to follow documentation from source code, and but the help is coming in…

Spring Boot Security Pre-authentication Example

Spring Security Pre Authentication Here you will see an example on Spring Security Pre-authentication. There are situations where you want to use Spring Security for authorization, but the user has already been reliably authenticated by some external system prior to accessing the application. In such situations where Spring Security Pre-authentication comes into picture I refer to these situations as “pre-authenticated”…

Spring JCache support

Introduction We will create an example on Spring JCache Support. Spring provides seamless JCache integration. In this example, I am using EhCache as a JCache specification provider. JCache is JSR 107 specification that provides annotations support such as @CacheResult, @CachePut, @CacheRemove and @CacheRemoveAll.

Logging Configuration in Spring Boot

Introduction In this tutorial I will show you how logging configuration in Spring Boot applications by overriding Spring Boot’s default logging mechanism by creating logback.xml file under src/main/resources directory. You may also read Simple log4j configuration in java and Log4j Configurations – Controlling Logging to Multiple Files By default, slf4j logging is already included in the Spring Boot application, you just…

Spring Batch – Quartz Scheduler

Quartz Scheduler This tutorial, spring batch quartz scheduler, will show you how to schedule the task repeatedly for reading a CSV file data and writing to XML file after some modification to the input CSV file using Quartz Scheduler API. I’ll build a service that imports data from a CSV file, transforms it with custom code, and store the final…