Showing posts with label spring framework. Show all posts
Showing posts with label spring framework. Show all posts

23 June, 2013

HATEOAS using Spring Framework

HATEOAS is a constraint of the REST application architecture which is very often ignored by developers. In many cases this is caused by the lack of support of frameworks used to build and expose RESTful services. In this post I will show how quickly we can add HATEOAS to RESTful web services using Spring Framework. Source code of the demo web application presented below are available for download, run it using mvn tomcat7:run command. Also you can read a nice presentation about why HATEOAS is important.

The Spring HATEOAS Library (currently in Release 0.6) provides API to help creating REST representations that follow the HATEOAS principle. The core problem it tries to address is link creation and representation assembly. Let's see the main steps required for enabling the HATEOAS for a RESTful web service:

02 June, 2013

Transaction synchronization callbacks in Spring Framework

Declarative transaction management simplifies considerably the development. Sometimes you want a little more control over transaction but at the same time you don't want to switch to programmatic transaction management. I'm talking about cases when you want to execute some activities only when transaction was completed successfully. For example: send an email with registration details, update a cache, send a message over network, etc. The tricky part here is that you want to perform these activities from a transactional method (a method marked with @Transactional which automatically will start and end the transaction).

Now let's see how we can achieve this in Spring Framework. TransactionSynchronizationManager is the central helper class that manages resources and transaction synchronizations per thread. It allows to register transaction synchronizations for the current thread if synchronization is active. TransactionSynchronization represents the actual transaction synchronization callback with methods like: afterCommit(), afterCompletion(int status), beforeCommit(boolean readOnly), beforeCompletion(), etc. In order to make things easier for use, we will create an after commit executor which will allow us to execute the code only after a successful commit.

14 September, 2011

CRUD RESTful services using Spring Framework

In this post I will show how to build CRUD (Create, Read, Update, Delete) REST-ful (Representational State Transfer) services using Spring Framework. Spring Framework version used in this example is 3.0.6.RELEASE. Apache Tomcat version used for testing the web services is 6.0.29 (default Tomcat version used by Tomcat Maven Plugin of Apache Maven 2.2.1). Project sources are available for download. So let’s start, step by step:

  1. First we have to define the resource on which the CRUD operation will be performed and how this resource will be represented. In our example we will use a resource that represents a User. The resource will be represented using XML (Extensible Markup Language). At the end of the post I will show how you can switch the resource representation to JSON (JavaScript Object Notation) or any other supported form.
  2. Create a Maven project. We will use Maven for project management and build. Maven POM is shown below:

31 July, 2011

How to register Infinispan listeners in Spring when using Infinispan as 2LC for Hibernate

In this post I will show how to register Infinispan listeners in Spring when Infinispan is used as second level cache (2LC) for Hibernate. So let’s start:
  1. Annotate your entity classes as cacheable. Pay attention to the cache region name ("cache_name") specified in the region attribute of Cache annotation.
    @Entity
    @Cache(region = "cache_name", usage = CacheConcurrencyStrategy.TRANSACTIONAL)
    @Cacheable
    @Table(name = "ENTITY_TABLE")
    public class MyEntity implements Serializable {
        // entity fields
    }
    
  2. Write your custom InfinispanRegionFactory as it is shown below. It extends from Hibernate InfinispanRegionFactory and its logic is very simple. The only thing which it does is to register listeners after its start. Pay attention to the named cache ("cache_name") to which listeners are registered. It should be the same as the cache region name used by your entities.