You are on page 1of 27

Spring Annotations: Contents:

Annotation Package Detail/Import statement


@Service import org.springframework.stereotype.Service;
@Repository import org.springframework.stereotype.Repository;
@Component import org.springframework.stereotype.Component;
@Autowired import org.springframework.beans.factory.annotation.Autowired;
@Transactional import org.springframework.transaction.annotation.Transactional;
@Scope import org.springframework.context.annotation.Scope;
Spring MVC Annotations
@Controller import org.springframework.stereotype.Controller;
@RequestMapping import org.springframework.web.bind.annotation.RequestMapping;
@PathVariable import org.springframework.web.bind.annotation.PathVariable;
@RequestParam import org.springframework.web.bind.annotation.RequestParam;
@ModelAttribute import org.springframework.web.bind.annotation.ModelAttribute;
@SessionAttributes import org.springframework.web.bind.annotation.SessionAttributes;
Spring Security Annotations
@PreAuthorize import org.springframework.security.access.prepost.PreAuthorize;

For spring to process annotations, add the following lines in your application-context.xml
file.
<context:annotation-config />
<context:component-scan base-package="...specify your package
name..." />
Spring supports both Annotation based and XML based configurations. You can
even mix them together. Annotation injection is performed before XML
injection, thus the latter configuration will override the former for properties
wired through both approaches.

@Service

Annotate all your service classes with @Service. All your business logic should be in Service
classes.
@Service
public class CompanyServiceImpl implements CompanyService {
...
}

@Repository
Annotate all your DAO classes with @Repository. All your database access logic should be
in DAO classes.
@Repository
public class CompanyDAOImpl implements CompanyDAO {
...
}

@Component

Annotate your other components (for example REST resource classes) with @Component.
@Component
public class ContactResource {
...
}
@Component is a generic stereotype for any Spring-managed component. @Repository,
@Service, and @Controller are specializations of @Component for more specific use cases,
for example, in the persistence, service, and presentation layers, respectively.

@Autowired

Let Spring auto-wire other beans into your classes using @Autowired annotation.
@Service
public class CompanyServiceImpl implements CompanyService {

@Autowired
private CompanyDAO companyDAO;

...
}
Spring beans can be wired by name or by type.

 @Autowire by default is a type driven injection. @Qualifier spring


annotation can be used to further fine-tune autowiring.
 @Resource (javax.annotation.Resource) annotation can be used for
wiring by name.

Beans that are themselves defined as a collection or map type cannot be injected
through @Autowired, because type matching is not properly applicable to them.
Use @Resource for such beans, referring to the specific collection or map bean
by unique name.
@Transactional

Configure your transactions with @Transactional spring annotation.


@Service
public class CompanyServiceImpl implements CompanyService {

@Autowired
private CompanyDAO companyDAO;

@Transactional
public Company findByName(String name) {

Company company = companyDAO.findByName(name);


return company;
}
...
}

To activate processing of Spring's @Transactional annotation, use the


<tx:annotation-driven/> element in your spring's configuration file.

The default @Transactional settings are as follows:

 Propagation setting is PROPAGATION_REQUIRED.


 Isolation level is ISOLATION_DEFAULT.
 Transaction is read/write.
 Transaction timeout defaults to the default timeout of the underlying transaction
system, or to none if timeouts are not supported.
 Any RuntimeException triggers rollback, and any checked Exception does not.

These default settings can be changed using various properties of the @Transactional spring
annotation.

Specifying the @Transactional annotation on the bean class means that it applies
to all applicable business methods of the class. Specifying the annotation on a
method applies it to that method only. If the annotation is applied at both the
class and the method level, the method value overrides if the two disagree.

@Scope

As with Spring-managed components in general, the default and most common scope for
autodetected components is singleton. To change this default behavior, use @Scope spring
annotation.
@Component
@Scope("request")
public class ContactResource {
...
}
Similarly, you can annotate your component with @Scope("prototype") for beans with
prototype scopes.
Please note that the dependencies are resolved at instantiation time. For
prototype scope, it does NOT create a new instance at runtime more than once. It
is only during instantiation that each bean is injected with a separate instance of
prototype bean.

Spring MVC Annotations


@Controller

Annotate your controller classes with @Controller.


@Controller
public class CompanyController {
...
}

@RequestMapping

You use the @RequestMapping spring annotation to map URLs onto an entire class or a
particular handler method. Typically the class-level annotation maps a specific request path
(or path pattern) onto a form controller, with additional method-level annotations narrowing
the primary mapping.
@Controller
@RequestMapping("/company")
public class CompanyController {

@Autowired
private CompanyService companyService;
...
}

@PathVariable

You can use the @PathVariable spring annotation on a method argument to bind it to the
value of a URI template variable. In our example below, a request path of /company/techferry
will bind companyName variable with 'techferry' value.
@Controller
@RequestMapping("/company")
public class CompanyController {

@Autowired
private CompanyService companyService;

@RequestMapping("{companyName}")
public String getCompany(Map<String, Object> map,
@PathVariable String companyName) {
Company company = companyService.findByName(companyName);
map.put("company", company);
return "company";
}
...
}

@RequestParam
You can bind request parameters to method variables using spring annotation
@RequestParam.
@Controller
@RequestMapping("/company")
public class CompanyController {

@Autowired
private CompanyService companyService;

@RequestMapping("/companyList")
public String listCompanies(Map<String, Object> map,
@RequestParam int pageNum) {
map.put("pageNum", pageNum);
map.put("companyList", companyService.listCompanies(pageNum));
return "companyList";
}
...
}
Similarly, you can use spring annotation @RequestHeader to bind request headers.

@ModelAttribute

An @ModelAttribute on a method argument indicates the argument should be retrieved from


the model. If not present in the model, the argument should be instantiated first and then
added to the model. Once present in the model, the argument's fields should be populated
from all request parameters that have matching names. This is known as data binding in
Spring MVC, a very useful mechanism that saves you from having to parse each form field
individually.
@Controller
@RequestMapping("/company")
public class CompanyController {

@Autowired
private CompanyService companyService;

@RequestMapping("/add")
public String saveNewCompany(@ModelAttribute Company company) {
companyService.add(company);
return "redirect:" + company.getName();
}
...
}

@SessionAttributes

@SessionAttributes spring annotation declares session attributes. This will typically list the
names of model attributes which should be transparently stored in the session, serving as
form-backing beans between subsequent requests.
@Controller
@RequestMapping("/company")
@SessionAttributes("company")
public class CompanyController {

@Autowired
private CompanyService companyService;
...
}
@SessionAttribute works as follows:

 @SessionAttribute is initialized when you put the corresponding attribute into model
(either explicitly or using @ModelAttribute-annotated method).
 @SessionAttribute is updated by the data from HTTP parameters when controller
method with the corresponding model attribute in its signature is invoked.
 @SessionAttributes are cleared when you call setComplete() on SessionStatus object
passed into controller method as an argument.

The following listing illustrate these concepts. It is also an example for pre-populating Model
objects.
@Controller
@RequestMapping("/owners/{ownerId}/pets/{petId}/edit")
@SessionAttributes("pet")
public class EditPetForm {

@ModelAttribute("types")

public Collection<PetType> populatePetTypes() {


return this.clinic.getPetTypes();
}

@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("pet") Pet pet,
BindingResult result, SessionStatus status) {
new PetValidator().validate(pet, result);
if (result.hasErrors()) {
return "petForm";
}else {
this.clinic.storePet(pet);
status.setComplete();
return "redirect:owner.do?ownerId="
+ pet.getOwner().getId();
}
}
}

Spring Security Annotations


@PreAuthorize

Using Spring Security @PreAuthorize annotation, you can authorize or deny a functionality.
In our example below, only a user with Admin role has the access to delete a contact.
@Transactional
@PreAuthorize("hasRole('ROLE_ADMIN')")
public void removeContact(Integer id) {
contactDAO.removeContact(id);
}

Hibernate JPA Annotations - Contents:


Annotation Package Detail/Import statement
@Entity import javax.persistence.Entity;
@Table import javax.persistence.Table;
@Column import javax.persistence.Column;
@Id import javax.persistence.Id;
@GeneratedValue import javax.persistence.GeneratedValue;
@Version import javax.persistence.Version;
@OrderBy import javax.persistence.OrderBy;
@Transient import javax.persistence.Transient;
@Lob import javax.persistence.Lob;
Hibernate Association Mapping Annotations
@OneToOne import javax.persistence.OneToOne;
@ManyToOne import javax.persistence.ManyToOne;
@OneToMany import javax.persistence.OneToMany;
@ManyToMany import javax.persistence.ManyToMany;
@PrimaryKeyJoinColumn import javax.persistence.PrimaryKeyJoinColumn;
@JoinColumn import javax.persistence.JoinColumn;
@JoinTable import javax.persistence.JoinTable;
@MapsId import javax.persistence.MapsId;
Hibernate Inheritance Mapping Annotations
@Inheritance import javax.persistence.Inheritance;
@DiscriminatorColumn import javax.persistence.DiscriminatorColumn;
@DiscriminatorValue import javax.persistence.DiscriminatorValue;

@Entity

Annotate all your entity beans with @Entity.

1@Entity
2public class Company implements Serializable {
3...
4}

@Table

Specify the database table this Entity maps to using the name attribute of @Table annotation.
In the example below, the data will be stored in 'company' table in the database.

1@Entity
2@Table(name = "company")
3public class Company implements Serializable {
...
4}
5

@Column

Specify the column mapping using @Column annotation.


1
@Entity
2@Table(name = "company")
3public class Company implements Serializable {
4
5 @Column(name = "name")
6 private String name;
7
8...
}
9

@Id

Annotate the id column using @Id.


1
@Entity
2 @Table(name = "company")
3 public class Company implements Serializable {
4
5 @Id
6 @Column(name = "id")
7 private int id;
8
...
9 }
10

@GeneratedValue

Let database generate (auto-increment) the id column.


1
2 @Entity
@Table(name = "company")
3 public class Company implements Serializable {
4
5 @Id
6 @Column(name = "id")
7 @GeneratedValue
private int id;
8
9 ...
10}
11

@Version
Control versioning or concurrency using @Version annotation.
1
@Entity
2 @Table(name = "company")
3 public class Company implements Serializable {
4
5 @Version
6 @Column(name = "version")
7 private Date version;
8
...
9 }
10

@OrderBy

Sort your data using @OrderBy annotation. In example below, it will sort all contacts in a
company by their firstname in ascending order.
1@OrderBy("firstName asc")
2private Set contacts;

@Transient

Annotate your transient properties with @Transient.

@Lob

Annotate large objects with @Lob.

Hibernate Association Mapping Annotations


Example App DB Schema
The database for this tutorial is designed to illustrate various association mapping concepts.
In RDBMS implementations, entities are joined using the following ways:

 Shared Primary Key


 Foreign Key
 Association Table

In our example app,

 Tables company and companyDetail have shared values for primary key. It is a one-
to-one assoication.
 Tables contact and contactDetail are linked through a foreign key. It is also a one to
one association.
 Tables contact and company are linked through a foriegn key in many-to-one
association with contact being the owner.
 Tables company and companyStatus are linked through a foreign key in many-to-one
association with company being the owner.

@OneToOne

 Use @PrimaryKeyJoinColumn for associated entities sharing the same


primary key.
 Use @JoinColumn & @OneToOne mappedBy attribute when foreign
key is held by one of the entities.
 Use @JoinTable and mappedBy entities linked through an association
table.
 Persist two entities with shared key using @MapsId
For entities Company and CompanyDetail sharing the same primary key, we can associate
them using @OneToOne and @PrimaryKeyJoinColumn as shown in the example below.

Notice that the id property of CompanyDetail is NOT annotated with @GeneratedValue. It


will be populated by id value of Company.
1
2
3 @Entity
@Table(name = "company")
4 public class Company implements Serializable {
5
6 @Id
7 @Column(name = "id")
8 @GeneratedValue
private int id;
9
10 @OneToOne(cascade = CascadeType.MERGE)
11 @PrimaryKeyJoinColumn
12 private CompanyDetail companyDetail;
13
14 ...
15}
16
@Entity
17@Table(name = "companyDetail")
18public class CompanyDetail implements Serializable {
19
20 @Id
21 @Column(name = "id")
22 private int id;
23
...
24}
25
26
For entities Contact and ContactDetail linked through a foriegn key, we can use @OneToOne
and @JoinColumn annotations. In example below, the id genereated for Contact will be
mapped to 'contact_id' column of ContactDetail table. Please note the usage of @MapsId for
the same.
1 @Entity
@Table(name = "contactDetail")
2 public class ContactDetail implements Serializable {
3
4 @Id
5 @Column(name = "id")
6 @GeneratedValue
private int id;
7
8 @OneToOne
9 @MapsId
10 @JoinColumn(name = "contactId")
11 private Contact contact;
12
13 ...
}
14
15@Entity
16@Table(name = "contact")
17public class Contact implements Serializable {
18
@Id
19 @Column(name = "ID")
20 @GeneratedValue
21 private Integer id;
22
23 @OneToOne(mappedBy = "contact", cascade = CascadeType.ALL)
24 private ContactDetail contactDetail;
25 ....
26}
27
28
29
30
31
Also note that the relationship between Company and CompanyDetail is uni-directional. On
the other hand, the relationship between Contact and Contact Detail is bi-directional and that
can be achieved using 'mappedBy' attribute.

The rationale to have one relationship as uni-directional and other as bi-directional in this
tutorial is to illustrate both concepts and their usage. You can opt for uni-directional or bi-
directional relationships to suit your needs.

@ManyToOne

 Use @JoinColumn when foreign key is held by one of the entities.


 Use @JoinTable for entities linked through an association table.

The two examples below illustrate many-to-one relationships. Contact to Company and
Company to CompanyStatus. Many contacts can belong to a company. Similary many
companies can share the same status (Lead, Prospect, Customer) - there will be many
companies that are currently leads.
1 @Entity
2 @Table(name = "contact")
3 public class Contact implements Serializable {
4 @ManyToOne
5 @JoinColumn(name = "companyId")
6 private Company company;
7
8 ...
9
10 }
11
12@Entity
@Table(name = "company")
13public class Company implements Serializable {
14
15 @ManyToOne
16 @JoinColumn(name = "statusId")
17 private CompanyStatus status;
18
19 ...
20
}
21
22
23

@OneToMany

 Use mappedBy attribute for bi-directional associations with ManyToOne


being the owner.
 OneToMany being the owner or unidirectional with foreign key - try to
avoid such associations but can be achieved with @JoinColumn
 @JoinTable for Unidirectional with association table

Please see the many-to-one relationship between Contact and Company above. Company to
Contact will be a one-to-many relationship. The owner of this relationship is Contact and
hence we will use 'mappedBy' attribute in Company to make it bi-directional relationship.
1
@Entity
2 @Table(name = "company")
3 public class Company implements Serializable {
4
5 @OneToMany(mappedBy = "company", fetch = FetchType.EAGER)
6 @OrderBy("firstName asc")
private Set contacts;
7
8
...
9
10 }
11
Again, for this tutorial, we have kept Company to CompanyStatus relationship as uni-
directional.

@ManyToMany

 Use @JoinTable for entities linked through an association table.


 Use mappedBy attribute for bi-directional association.

@PrimaryKeyJoinColumn

@PrimaryKeyJoinColumn annotation is used for associated entities sharing the same primary
key. See OneToOne section for details.
1 @Entity
2 @Table(name = "company")
public class Company implements Serializable {
3
4 @Id
5 @Column(name = "id")
6 @GeneratedValue
7 private int id;
8
@OneToOne(cascade = CascadeType.MERGE)
9 @PrimaryKeyJoinColumn
10 private CompanyDetail companyDetail;
11
12 ...
13}
14
15

@JoinColumn

Use @JoinColumn annotation for one-to-one or many-to-one associations when foreign key
is held by one of the entities. We can use @OneToOne or @ManyToOne mappedBy attribute
for bi-directional relations. Also see OneToOne and ManyToOne sections for more details.
1@ManyToOne
2@JoinColumn(name = "statusId")
3private CompanyStatus status;

@JoinTable

Use @JoinTable and mappedBy for entities linked through an association table.

@MapsId

Persist two entities with shared key (when one entity holds a foreign key to the other) using
@MapsId annotation. See OneToOne section for details.
1@OneToOne
2@MapsId
3@JoinColumn(name = "contactId")
4private Contact contact;

Hibernate Inheritance Mapping Annotations


To understand Inheritance Mapping annotations, you must first understand Inheritance
Mapping in Hiberate in detail. Once you understand Inheritance mapping concepts, please
review below for annotations to be used.

 table per class hierarchy - single table per Class Hierarchy Strategy: the <subclass>
element in Hibernate

1 @Entity
2 @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="planetype",
3 discriminatorType=DiscriminatorType.STRING )
4
5 @DiscriminatorValue("Plane")
6 public class Plane { ... }
7
8 @Entity
9 @DiscriminatorValue("A320")
public class A320 extends Plane { ... }
10

 table per class/subclass - joined subclass Strategy: the <joined-subclass> element in


Hibernate

1@Entity
2@Inheritance(strategy=InheritanceType.JOINED)
3public class Boat implements Serializable { ... }
4
5@Entity
6@PrimaryKeyJoinColumn
public class Ferry extends Boat { ... }
7

 table per concrete class - table per Class Strategy: the <union-class> element in
Hibernate

1 @Entity
2 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
3 public class Flight implements Serializable { ... }

Note: This strategy does not support the IDENTITY generator strategy:
the id has to be shared across several tables. Consequently, when using
this strategy, you should not use AUTO nor IDENTITY.

@Inheritance

See Hibernate Inheritance Mapping Annotations section for details.


1@Entity
2@Inheritance(strategy=InheritanceType.SINGLE_TABLE)

@DiscriminatorColumn

See Hibernate Inheritance Mapping Annotations section for details.


1@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
2@DiscriminatorColumn(name="planetype",
3discriminatorType=DiscriminatorType.STRING )

@DiscriminatorValue

See Hibernate Inheritance Mapping Annotations section for details.


1 @Entity
2 @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="planetype",
3 discriminatorType=DiscriminatorType.STRING )
4
5 @DiscriminatorValue("Plane")
6 public class Plane { ... }
7
8 @Entity
9 @DiscriminatorValue("A320")
public class A320 extends Plane { ... }
10

RESTful Web Service - JAX-RS Annotations - Contents:


Annotation Package Detail/Import statement
@GET import javax.ws.rs.GET;
@Produces import javax.ws.rs.Produces;
@Path import javax.ws.rs.Path;
@PathParam import javax.ws.rs.PathParam;
@QueryParam import javax.ws.rs.QueryParam;
@POST import javax.ws.rs.POST;
@Consumes import javax.ws.rs.Consumes;
@FormParam import javax.ws.rs.FormParam;
@PUT import javax.ws.rs.PUT;
@DELETE import javax.ws.rs.DELETE;

As stated earlier in Example Application, we are using Jersey for RESTful Web services and
JAX-RS annotations.

REST follows one-to-one mapping between create, read, update, and delete
(CRUD) operations and HTTP methods.

 To create a resource on the server, use POST.


 To retrieve a resource, use GET.
 To change the state of a resource or to update it, use PUT.
 To remove or delete a resource, use DELETE.

@GET

Annotate your Get request methods with @GET.


1@GET
2public String getHTML() {
3 ...
4}
@Produces

@Produces annotation specifies the type of output this method (or web service) will produce.
1@GET
2@Produces("application/xml")
3public Contact getXML() {
4 ...
5}
1@GET
2@Produces("application/json")
3public Contact getJSON() {
4 ...
5}

@Path

@Path annotation specify the URL path on which this method will be invoked.
1@GET
2@Produces("application/xml")
3@Path("xml/{firstName}")
4public Contact getXML() {
5 ...
}
6

@PathParam

We can bind REST-style URL parameters to method arguments using @PathParam


annotation as shown below.
1
@GET
2@Produces("application/xml")
3@Path("xml/{firstName}")
4public Contact getXML(@PathParam("firstName") String firstName) {
5 Contact contact = contactService.findByFirstName(firstName);
6} return contact;
7
1
@GET
2@Produces("application/json")
3@Path("json/{firstName}")
4public Contact getJSON(@PathParam("firstName") String firstName) {
5 Contact contact = contactService.findByFirstName(firstName);
6} return contact;
7

@QueryParam

Request parameters in query string can be accessed using @QueryParam annotation as shown
below.
1@GET
@Produces("application/json")
2@Path("json/companyList")
3public CompanyList getJSON(@QueryParam("start") int start,
4@QueryParam("limit") int limit) {
CompanyList list = new CompanyList(companyService.listCompanies(start,
5limit));
6 return list;
7}
The example above returns a list of companies (with server side pagination) which can be
displayed with rich clients implemented using Ext-js or jQuery. You can read more more
about setting up ExtJS grid panel with remote sorting and pagination using Hibernate.

@POST

Annotate POST request methods with @POST.


1@POST
2@Consumes("application/json")
3@Produces("application/json")
4public RestResponse<Contact> create(Contact contact) {
5...
}
6

@Consumes

The @Consumes annotation is used to specify the MIME media types a REST resource can
consume.
1
@PUT
2@Consumes("application/json")
3@Produces("application/json")
4@Path("{contactId}")
5public RestResponse<Contact> update(Contact contact) {
6...
}
7

@FormParam

The REST resources will usually consume XML/JSON for the complete Entity Bean.
Sometimes, you may want to read parameters sent in POST requests directly and you can do
that using @FormParam annotation. GET Request query parameters can be accessed using
@QueryParam annotation.
1@POST
2public String save(@FormParam("firstName") String firstName,
3 @FormParam("lastName") String lastName) {
4 ...
}
5

@PUT

Annotate PUT request methods with @PUT.


1@PUT
@Consumes("application/json")
2@Produces("application/json")
3@Path("{contactId}")
4public RestResponse<Contact> update(Contact contact) {
5...
}
6
7

@DELETE

Annotate DELETE request methods with @DELETE.


1@DELETE
2@Produces("application/json")
3@Path("{contactId}")
public RestResponse<Contact> delete(@PathParam("contactId") int contactId)
4{
5...
6}

JAXB Annotations - Contents:


Annotation Package Detail/Import statement

@XmlRootElement import javax.xml.bind.annotation.XmlRootElement;

@XmlElement import javax.xml.bind.annotation.XmlElement;

@XmlType import javax.xml.bind.annotation.XmlType;

@XmlTransient import javax.xml.bind.annotation.XmlTransient;

@XmlSeeAlso import javax.xml.bind.annotation.XmlSeeAlso;

Using JAXB and JPA Annotations in Conjunction

Using JAX-RS Annotations with JAXB and JPA Annotations

As stated earlier in Example Application, we are using JAXB to convert our Entities to XML or JSON
format, so our rich clients like Ext-js or jQuery can easily process and present the data.

@XmlRootElement

Define the root element for the XML to be produced with @XmlRootElement JAXB annotation. The
name of the root XML element is derived from the class name.

1@XmlRootElement
2public class Contact implements Serializable {

3...
}
4

You can also specify the name of the root element of the XML using its name attribute, for example
@XmlRootElement(name = "CompanyContact")

@XmlElement

Annotate all fields that needs to be included in XML/JSON output with @XMLElement.

1 @XmlElement
2 public String getName() {

3 return name;

4 }

Either annotate all fields or all getter methods in your Entity bean. A mix of both is not
supported. Add @XmlAccessorType(XmlAccessType.FIELD) at the class level if you want
to annotate private fields instead of getter methods.

@XmlType

Specify the order in which XML elements or JSON output will be produced.

1@XmlRootElement

2@XmlType(propOrder = { "id", "firstName", "lastName", "email", "telephone"


})
3public class Contact implements Serializable {

4...

5}

The above @XmlType annotation will produce the following XML.

1
<contact>
2 <id>38</id>
3 <firstname>FirstName</firstname>

4 <lastname>LastName</lastname>

5 <email>dummyEmail@techferry.com</email>

<telephone>1111111111</telephone>
6
</contact>
7

Similarly, it will produce the following JSON.


1{"id":"38","firstName":"FirstName","lastName":"LastName",

2"email":"dummyEmail@techferry.com","telephone":"1111111111"}

@XmlTransient

Annotate fields that we do not want to be included in XML or JSON output with @XMLTransient.

1@XmlTransient

2public Date getVersion() {

3 return version;

4}

@XmlSeeAlso

Use @XmlSeeAlso annotation when we want another Entity bean included in the XML output. In our
example below, CompanyList bean refers to Company bean and the XML output should include XML
generated from Company Entity too.

1
@XmlRootElement(name = "List")
2
@XmlSeeAlso(Company.class)
3 public class CompanyList {

5 @XmlElement(name = "companyList")

6 public List<Company> getList() {

7 return list;

}
8
...
9
}
10

To include more than 1 classes, we can use @XmlSeeAlso JAXB annotation as:
@XmlSeeAlso({ A.class, B.class })

Using JAXB and JPA Annotations in Conjunction

If you have reviewed both Hibernate - JPA Annotations and JAXB Annotations, the following snippet
illustrates usage of both JAXB and JPA annotations in the same entity Contact.
1 @Entity

2 @Table(name = "CONTACT")

@XmlRootElement
3
@XmlType(propOrder = { "id", "firstName", "lastName", "email",
4 "telephone" })
5 public class Contact implements Serializable {

7 @Id

8 @Column(name = "ID")

@GeneratedValue
9
private Integer id;
10

11
@Column(name = "firstName")
12
private String firstName;
13

14
@Column(name = "lastName")
15 private String lastName;
16

17 @Column(name = "EMAIL")
18 private String email;

19

20 @Column(name = "TELEPHONE")

21 private String telephone;

22
@Version
23
@Column(name = "version")
24
private Date version;
25

26
@ManyToOne
27
@JoinColumn(name = "companyId")
28 private Company company;
29

30 @OneToOne(mappedBy = "contact", cascade = CascadeType.ALL)


31 private ContactDetail contactDetail;
32

33 @XmlTransient

34 public Company getCompany() {

return company;
35
}
36

37
public void setCompany(Company company) {
38
this.company = company;
39
}
40

41 @XmlTransient
42 public ContactDetail getContactDetail() {
43 return contactDetail;

44 }

45

46 public void setContactDetail(ContactDetail contactDetail) {

this.contactDetail = contactDetail;
47
}
48

49
@XmlTransient
50
public Date getVersion() {
51
return version;
52 }
53

54 public void setVersion(Date version) {

55 this.version = version;

56 }

57

58 @XmlElement

public Integer getId() {


59
return id;
60
}
61

62
63 public void setId(Integer id) {

64 this.id = id;

}
65

66
@XmlElement
67
public String getFirstName() {
68
return firstName;
69
}
70

71 public void setFirstName(String firstName) {


72 this.firstName = firstName;
73 }

74

75 @XmlElement

76 public String getLastName() {

return lastName;
77
}
78

79
public void setLastName(String lastName) {
80
this.lastName = lastName;
81
}
82

83 @XmlElement
84 public String getEmail() {

85 return email;

86 }

87

88 public void setEmail(String email) {

this.email = email;
89
}
90

91
@XmlElement
92
public String getTelephone() {
93
94 return telephone;

95 }

96
public void setTelephone(String telephone) {
97
this.telephone = telephone;
98
}
99

100
}
101

102

103

104

105

106

107

Using JAX-RS Annotations with JAXB and JPA Annotations

This section assumes that you have reviewed RESTful JAX-RS Annotations, Hibernate - JPA
Annotations and JAXB Annotations. Also see the section above on using JAXB and JPA Annotations in
Conjunction.

Now that you have an entity bean containing both JAXB and JPA Annotations which is capable of
doing data exchange with database and coverting it to required JSON/XML format, the next step is
to send this data to rich clients using jQuery or Ext-js. In your REST based web-service methods,
return the Contact entity bean as shown below. Jersey, JAXB will take care of data conversion and
appropriate response generation.

1
@GET
2@Produces("application/xml")

3@Path("xml/{firstName}")

4public Contact getXML(@PathParam("firstName") String firstName) {

5 Contact contact = contactService.findByFirstName(firstName);

return contact;
6
}
7
@GET
1
@Produces("application/json")
2@Path("json/{firstName}")

3public Contact getJSON(@PathParam("firstName") String firstName) {


Contact contact = contactService.findByFirstName(firstName);
4
return contact;
5
}
6

Spring jUnit Annotations - Contents:


Annotation Package Detail/Import statement

@RunWith import org.junit.runner.RunWith;

@ContextConfiguration import org.springframework.test.context.ContextConfiguration;

@Test import org.junit.Test;

@DirtiesContext import org.springframework.test.annotation.DirtiesContext;

@Timed import org.springframework.test.annotation.Timed;

We are now ready to test our Spring based application using jUnit and Spring Unit testing
framework. The following jUnit and Spring annotations will be used to accomplish this.

@RunWith

When a class is annotated with @RunWith or extends a class annotated with @RunWith, JUnit will
invoke the class it references to run the tests in that class instead of the runner built into JUnit. Let
us configure jUnit to use Spring jUnit Class runner.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/spring-servlet-test.xml"})
public class CompanyServiceTest {
...
}

@ContextConfiguration

Set the spring ApplicationContext for your test classes using @ContextConfiguration annotation.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/spring-servlet-test.xml"})
public class CompanyServiceTest {
...
}

@ContextConfiguration provides support for inheriting resource locations or


configuration classes declared by superclasses by default.

@Test

Annotate all your jUnit unit tests with @Test. Also note that we can wire other spring beans in our
jUnit test classes using @Autowired annotation.
@Autowired
private CompanyService companyService;

@Test
public void testFindByName() {
Company company = companyService.findByName("techferry");
if (company != null) {
assertEquals("prospect", company.getStatus().getName());
}
}

@DirtiesContext

Annotate @DirtiesContext to indicate that it dirties the ApplicationContext. This will trigger context
reloading before execution of next test.

@Timed

Indicates that the annotated test method must finish execution in a specified time period (in
milliseconds). If the text execution time exceeds the specified time period, the test fails.
@Timed(millis=1000)

You might also like