Groovy Language

  • Groovy can run on JVM
  • Groovy scripts can be executed using groovy jar file.
  • Annotations are supported in groovy.

Groovy installation.

  • Binary download link
  • The binary can be run from command prompt
  • Set GROOVY_HOME, GROOVY_HOME\bin variable as enviroment variable.
  • After installing and running in the command line you get the groovy shell(groovysh).
  • groovy> println ” hello helical”

Source code in groovy.

  • The extension for groovy source file is .groovy
  • We can write either script or we may also encapsulate the code as class definition inside this file.
  • By default groovy class is subclassed by java.lang.Object.
  • Java class and its objects can access groovy script class and object and vice versa.
  • Using groovyc we can compile the source code.
  • Plain Old Groovy Objects (POGO).

Intersting facts about groovy.

  • Groovy doesnot require ; at the end of statement.
  • Last expression of the method is returned by default. So we may not explicitly use return keyword.
  • Maps, list and regular expression readily available in groovy.
  • The following package are readily available. (groovy.lang.*, groovy.util.* , java.lang.*, j ava.util.*,*,*, java.math.BigInteger, java.math.BigDecimal)
  • Its classes and methods are by-default public
  • Automatic getter and setter is created for the fields in groovy class.
  • Unlike java == operator in groovy checks for contents.
  • The is() may be used to check if two variables/object refer the same.
  • The parameters inside a function are optional.
  • Gpath is expression language to evaluate nested structure.

Data types in Groovy.

    • The keyword def is used to define variable. We can also use specific type for variable declaration. Example int marks, String company. etc
    • Range data type is a Collection. Ranges consists of two values separated by two dots.
for (i in 0..9) {
  println ("Hello $i")

assert 'L'..'P' == ['L', 'M', 'N', 'O','P']

String & Gstring.

  • ‘Strings may be enclosed inside single quote ‘.
  • “Mehod() calls and ${variable} substitution inside a string is known as Gstring”.
  • ”’Tripple single quotes can be used for multi line string”’
  • “”” Multi line Gstring can be enclosed in tripple quotes”””
  • /regularexpression/

Methods in Groovy.

  • Groovy supports operator overloading. – operator can be used to substract string, << can be used for string concatination.
  • Groovy string has .toUrl() method that can be directly used to convert a string to Url encoding string.



package packageName
class ClassName {
  static void main(def args){
    def hetroList= [1,2,"hi","45"]
    hetroList.each{ println it }

JAX-RS and Jersey api

JAX-RS and jersey

JSR 311 defines REST support in java. JAX-RS uses annotations to define the REST in java classes.

What is Jersey?

  • Jersey  provieds api/library for JSR 311. We can use it in implementing Restful webservices in a Java servlet container.
  • In server side it helps in scanning predefined classes and identifies RESTful resources. In web.xml we need to register the servlet for web application.
  • It also provies a client library to communicate with a RESTful webservice.


How does it works?
Let say the application has the following URL as the request from the client/browser.

The servlet maps the required class and method to give correct response to the above request.
The annotations in the classes helps the servlet to pick the right mapping.

Using the internal library Java Architecture for XML Binding (JAXB) we can have the
JAX-RS supports the creation of XML and JSON.
Annotation Description 

Annotation  Description Package Detail
@GET helps in handle the GET request (
@POST helps in handling the POST request (
@Produces it handles the output type that will be generated (
@Path it handles the request mapping for specific url (
@PathParam helps to  inject values from the URL into a method parameter. (
@QueryParam if the request have any query parameters then we can use this to handle using this annotation (
@Consumes it specifies MIME media types a REST resource can consume. (
@FormParam the REST resources generally consume XML/JSON. Using this we can read form parameters sent in POST requests. (
@PUT handles the PUT method (
@DELETE handles the DELETE method (


Maven jersey dependency.
A quick google search will give you maven dependency. Depending upon the project requirement you can find one.

Maven dependency for jersey-servlet


Maven dependency for client


Code snippets

/Maps the path for BaseURL/rest/myMapping
package com.helical.restExample

public class SampleService {
   public String plainMessage() {
    return "REST WORLD";

   public String xmlMessage() {
     return "<?xml version=\"1.0\"?>" + "<message>REST WORLD" + "</message>";

    public String jsonMessage() {
    return "{\"mesaage\" : \"rest world\"}";

   public String htmlMessage() {
    return "<html> " + "<title>" + "REST WORLD" + "</title>"
     + "<body><h1>" + "Welcome to the Rest world" + "</body></h1>" + "</html> ";




Registering the api

In the web.xml we need to configure the jersey servlet to help the mapping

<servlet-name>Jersey REST Service</servlet-name>
<servlet-name>Jersey REST Service</servlet-name>


This is good to go. We can run the webservice application to get the desired result.

Sources internet