Entries

Showing posts with the label spring

Asynchrouns and Transactional Event Listeners in Spring

The built-in event publication functionality exists from the early Spring versions and it is still useful for handling basic communication between Spring components in the same application context. In general, the application can generate application events (that can be arbitrary objects) and listen to them. The whole mechanism is really simple: using ApplicationPublisher you publish events and using EventListener you handle them. What I find especially useful is asynchronous and transactional event listeners .

Lombok - you should definitely give it a try

Lombok is not a new thing in a Java ecosystem, but I must admit I always underestimated its value until I tried it or I was “convienced” to try it. I did not see much value in adding a library that generates code that can be easily generated by any modern IDE these days. So I ignored the library and I have been writing or generating tons of boilerplate code. Not anymore. In 2016 I joined a Spring-based project where project Lombok was already in place. And since then I can’t work without Lombok anymore… Why?

Skip SSL certificate verification in Spring Rest Template

How to skip SSL certificate verification while using Spring Rest Template? Configure Rest Template so it uses Http Client to create requests.

HOW-TO: Register components using @Conditional and Condition in Spring

@Profile annotation in Spring can be used on any Spring components (e.g. @Component , @Service , @Configuration etc.) that are candidates for auto-detection. @Profile annotation accepts a single profile or a set of profiles that must be active in order to make the annotated component eligible for auto-detection. For a given @Profile({"p1", "!p2"}) , registration will occur if profile p1 is active or if profile p2 is not active. The or is crucial here. But how to achieve this with @Profile : we want to activate a given component if profile p1 is active and if profiles p2 and p3 are both inactive?

JAX-RS 2.x vs Spring MVC: Returning an XML representation of a list of objects

JSON is King as it goes to all kinds of REST* APIs, but still you may need to expose multiple representations, including XML. With both JAX-RS and Spring MVC this is very simple. Actually, the only thing to do is to annotate POJOs returned from the API call with JAXB annotation and that’s it. But when it goes to serializing a list of objects, JAX-RS will do a bit better than Spring MVC, in my opinion. Let’s see.

HOW-TO: Spring Boot and Thymeleaf with Maven

Spring Boot is a great piece of software allowing you to bootstrap Spring application within a few seconds. And it really works. As little configuration as possible to get started. And still possible to change the defaults. Let's see how easily is to bootstrap Spring MVC with Thymeleaf and Maven and work with it in IntelliJ.

CSRF protection in Spring MVC, Thymeleaf, Spring Security application

Cross-Site Request Forgery (CSRF) is an attack which forces an end user to execute unwanted actions on a web application in which he/she is currently authenticated. Preventing CSRF attacks in Spring MVC / Thymeleaf application is fairly easy if you use Spring Security 3.2 and above.

Spring MVC @PathVariable Tips

@PathVariable annotation is one of the Spring MVC features that allows creating RESTful Web application much easier. It indicates that a handler method parameter should be bound to a URI template. In this post I will present two useful tips for working with this annotation.

HOW-TO: Custom error pages in Tomcat with Spring MVC

Default Tomcat error pages look scary. In addition, they may expose valuable information including server version and exception stack trace. Servlet specification provides a way to configure an exceptional behavior through web.xml. One can configure either reaction on a specific Java exception or to a selected Http response code(s).

HOW-TO: Method-level validation in Spring with @Validated annotation

@Validated is a Spring’s specific variant of JSR-303’s javax.validation.Valid, supporting the specification of validation groups. @Validated can be used with Spring MVC handler methods arguments as well as with method level validation. In this article, you will learn how to use @Validated annotation with method level validation in Spring.

Spring MVC 3.1 Quickstart Maven Archetype

Spring 3.1 brings many enhancements to its MVC framework. Many times I need to start some basic project to consult new things on the project for the presentation, blog post or just for myself. In order to make the creation of each project repeatable and easy process I decided to create Maven archetype for that puropose. Currently it is a snapshot version that support basic Spring MVC 3.1 project with JPA2.0 and Apache Tiles. See more details on the spring-mvc-quickstart-archetype project page how to get started with it . If you have any suggestions or you would like to join this project, please let me know.

(My) Top 5 enhancements of Spring MVC 3.1

After many months of development Spring 3.1 has been released. The release is shipped with some exciting features like caching abstraction, bean profiles and container configuration simplifications. This release introduced also many useful enhancments to Spring MVC. Read my full article at GOYELLO blog here: http://blog.goyello.com/2011/12/16/enhancements-spring-mvc31

Spring 3.1 HttpPutFormContentFilter: supporting PUT request with form encoded data

Spring MVC 3.0 introduced many useful features to work with RESTful interface, including URI templates, content negotation, http method conversion and more. While dealing with RESTful or REST-like API you may like to utilize REST key principle - its Uniform Interface.

Spring 3.1 MVC: xml-free configuration in Servlet 3.0 environment

Starting from Spring 3.1.0.M2 you can configure Servlet Context programatically in Servlet 3.0 environment (Tomcat 7 for example), with no web.xm l and no xml at all. This article demonstrate working Hello World example with xml-free web application configuration. Update: See Spring MVC Quickstart Maven Archetype (no-xml Spring MVC 4 web application): https://github.com/kolorobot/spring-mvc-quickstart-archetype to get started with Spring MVC. The project is actively maintained and just got updated: http://blog.codeleak.pl/2016/01/spring-mvc-4-quickstart-maven-archetype.html To start with the project I used STS and I created new Template Project ( File > New > Spring Template Project > Spring MVC Project ). Once the project was created I made some small modification to the POM file: removed Spring Roo dependencies - why they are there - I don't know changed Spring version to 3.1.0.M2 - to have new functionality in place change maven-war-plugin configuration - s...

Spring Security 3.1: Enhanced security namespace configuration

Security namespace configuration in Spring Security 3.1 improved. The change that made my life easier is the possibility of using multiple <http> to configure security in web application. Each <http> can now configure separate filter chain for different request pattern. This is very useful when you have a web application that consist of standard web application and the API and you want API to be accessible only with basic authentication. To achieve that in Spring Security 3.1 you need to define security configuration as following: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sec="http://www.springframework.org/schema/security" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/s...