In a Spring+Jersey+Hibernate RESTful webapplication, we can spot various J2EE components - JTA, JPA, Java Bean Validation, JSON-B API for Binding, JAXB, JAX-RS etc.
As a whole application, we need all the above components work together. Spring is the gluing technology that achieves such purpose with the help of annotation or xml or java based configuration.
Example:
1. JTA components can be enabled with @Transaction
2. @Entity, @Table, @Column etc enable JPA
3. @Valid enables Bean-validation
4. @Path, @GET, @POST etc assist JAX-RS
5. @JsonProperty part of Jackson - an alternative api such as GSON etc for JSON-B
Note: Jackson, GSON etc are not actual standard implementations of JSON-B.
As a whole application, we need all the above components work together. Spring is the gluing technology that achieves such purpose with the help of annotation or xml or java based configuration.
Example:
1. JTA components can be enabled with @Transaction
2. @Entity, @Table, @Column etc enable JPA
3. @Valid enables Bean-validation
4. @Path, @GET, @POST etc assist JAX-RS
5. @JsonProperty part of Jackson - an alternative api such as GSON etc for JSON-B
Note: Jackson, GSON etc are not actual standard implementations of JSON-B.
- Though, these layers are coupled together, the application may not run into any failure if one of the components does not work properly. Consider the following scenario:
- RestController receives the JSON request, validates/maps to the entity, forwards it to Service layer. A transaction gets initiated and data gets persisted to database in Service and DAO layers respectively.
- The above scenario is implemented with the help of annotations - @Valid for validation, @JsonProperty for Mapping to Bean, @Transactional and @Entity etc in service and DAO layers.
- What happens if we miss @Valid annotation and send invalid data to the application. Well, we may expect the request may get failed since validation semantics are not invoked. But in fact there will not be any such exceptional behaviour but the invalid data gets saved to the database. Though we dont like to persist invalid data in database, the application behaviour is as per the expectation.
- The way J2EE components are designed to work is so convenient that each layer works independently of others. Any specific layer does its job with the data that it receives from previous layer irrespective of whether the data is valid.