SlideShare a Scribd company logo
1 of 24
Download to read offline
Restful applications using Spring MVC




Kamal Govindraj
TenXperts Technologies
About Me
   Programming for 13 Years
   Architect @ TenXperts Technologies
   Trainer / Consultant @ SpringPeople
    Technologies
   Enteprise applications leveraging open source
    frameworks (spring,hibernate, gwt,jbpm..)
   Key contributor to InfraRED & Grails jBPM
    plugin (open source)
Agenda
   What is REST?
   REST Concepts
   RESTful Architecture Design
   Spring @MVC
   Implement REST api using Spring @MVC 3.0
What is REST?
   Representational State Transfer
   Term coined up by Roy Fielding
    −   Author of HTTP spec
   Architectural style
   Architectural basis for HTTP
    −   Defined a posteriori
Core REST Concepts
 Identifiable Resources
 Uniform interface

 Stateless conversation

 Resource representations

 Hypermedia
Identifiable Resources
   Everything is a resource
    −   Customer
    −   Order
    −   Catalog Item
   Resources are accessed by URIs
Uniform interface
   Interact with Resource using single, fixed
    interface
   GET /orders - fetch list of orders
   GET /orders/1 - fetch order with id 1
   POST /orders – create new order
   PUT /orders/1 – update order with id 1
   DELETE /orders/1 – delete order with id 1
   GET /customers/1/order – all orders for
    customer with id 1
Resource representations
   More that one representation possible
    −   text/html, image/gif, application/pdf
   Desired representation set in Accept HTTP
    header
    −   Or file extension
   Delivered representation show in Content-
    Type
   Access resources through representation
   Prefer well-known media types
Stateless conversation
   Server does not maintain state
    −   Don’t use the Session!
   Client maintains state through links
   Very scalable
   Enforces loose coupling (no shared session
    knowledge)
Hypermedia
   Resources contain links
   Client state transitions are made through these links
   Links are provided by server
   Example

    <oder ref=”/order/1”>
        <customer ref=”/customers/1”/>
        <lineItems>
             <lineItem item=”/catalog/item/125” qty=”10”/>
             <lineItem item=”/catalog/item/150” qty=”5”>
        </lineItems>
    </oder>
RESTful Architecture
RESTful application design
   Identify resources
    −   Design URIs
   Select representations
    −   Or create new ones
   Identify method semantics
   Select response codes
Implement Restful API using Spring MVC
Spring @MVC
@Controller
public class AccountController {
     @Autowired AccountService accountService;

     @RequestMapping("/details")
     public String details(@RequestParam("id")Long id,
                     Model model) {
          Account account = accountService.findAccount(id);
          model.addAttribute(account);
          return "account/details";
     }
}

account/details.jsp
    <h1>Account Details</h1>
    <p>Name : ${account.name}</p>
    <p>Balance : ${account.balance}</p>

../details?id=1
RESTful support in Spring 3.0
   Extends Spring @MVC to handle URL
    templates - @PathVariable
   Leverages Spring OXM module to provide
    marshalling / unmarshalling (castor, xstream,
    jaxb etc)
   @RequestBody – extract request body to
    method parameter
   @ResponseBody – render return value to
    response body using converter – no views
    required
REST controller
@Controller public class AccountController {

      @RequestMapping(value="/accounts",method=RequestMethod.GET)
      @ResponseBody public AccountList getAllAcccount() {
      }

      @RequestMapping(value="/accounts/${id}",method=RequestMethod.GET)
      @ResponseBody public Account getAcccount(@PathVariable("id")Long id) {
      }

      @RequestMapping(value="/accounts/",method=RequestMethod.POST)
      @ResponseBody public Long createAcccount(@RequestBody Account account) {
      }

      @RequestMapping(value="/accounts/${id}",method=RequestMethod.PUT)
      @ResponseBody public Account updateAcccount(@PathVariable("id")Long id, @RequestBody Account account) {
      }

      @RequestMapping(value="/accounts/${id}",method=RequestMethod.DELETE)
      @ResponseBody public void deleteAcccount(@PathVariable("id")Long id) {
      }
}
web.xml

<servlet>
    <servlet-name>rest-api</servlet-name>
    <servlet-class>org....web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>rest-api</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
rest-api-servlet-context.xml

<bean id="marshaller" class="...oxm.xstream.XStreamMarshaller">
   <property name="aliases">
       <map>
          <entry key="category" value="...domain.Category"/>
          <entry key="catalogItem" value="...domain.CatalogItem"/>
       </map>
   </property>
</bean>
rest-api-servlet-context.xml
<bean
    class="...mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <util:list>
              <bean
                    class="...http.converter.xml.MarshallingHttpMessageConverter">
                    <constructor-arg ref="marshaller" />
              </bean>
              <bean
                    class="...http.converter.StringHttpMessageConverter" />
        </util:list>
    </property>
</bean>
Invoke REST services
   The new RestTemplate class provides client-
    side invocation of a RESTful web-service
     HTTP                       RestTemplate Method
    Method
    DELETE    delete(String url, String... urlVariables)
    GET       getForObject(String url, Class<t> responseType, String …
              urlVariables)
    HEAD      headForHeaders(String url, String… urlVariables)
    OPTIONS   optionsForAllow(String url, String… urlVariables)
    POST      postForLocation(String url, Object request, String…
              urlVariables)

    PUT       put(String url, Object request, String…urlVariables)
REST template example

AccountList accounts = restTemplate.getForObject("/accounts/",AccountList.class);

Account account = restTemplate.getForObject("/accounts/${id}",Account.class ,"1");

restTemplate.postForLocation("/accounts/", account);

restTemplate.put("/accounts/${id}",account,"1");

restTemplate.delete("/accounts/${id}", "1");
Demo
Questions?
Thank You

More Related Content

What's hot (20)

Spring Boot
Spring BootSpring Boot
Spring Boot
 
Introduction to Spring Framework and Spring IoC
Introduction to Spring Framework and Spring IoCIntroduction to Spring Framework and Spring IoC
Introduction to Spring Framework and Spring IoC
 
Spring boot
Spring bootSpring boot
Spring boot
 
Struts framework
Struts frameworkStruts framework
Struts framework
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
 
Spring MVC Framework
Spring MVC FrameworkSpring MVC Framework
Spring MVC Framework
 
Spring Framework
Spring FrameworkSpring Framework
Spring Framework
 
Spring Boot Tutorial
Spring Boot TutorialSpring Boot Tutorial
Spring Boot Tutorial
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
 
Spring ppt
Spring pptSpring ppt
Spring ppt
 
Java Spring Framework
Java Spring FrameworkJava Spring Framework
Java Spring Framework
 
Spring boot - an introduction
Spring boot - an introductionSpring boot - an introduction
Spring boot - an introduction
 
Struts
StrutsStruts
Struts
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
 
Servlets
ServletsServlets
Servlets
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 
Spring introduction
Spring introductionSpring introduction
Spring introduction
 

Viewers also liked

RESTful Web Services with Spring MVC
RESTful Web Services with Spring MVCRESTful Web Services with Spring MVC
RESTful Web Services with Spring MVCdigitalsonic
 
Spring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSpring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSam Brannen
 
Microservices with Spring Boot
Microservices with Spring BootMicroservices with Spring Boot
Microservices with Spring BootJoshua Long
 
That old Spring magic has me in its SpEL
That old Spring magic has me in its SpELThat old Spring magic has me in its SpEL
That old Spring magic has me in its SpELCraig Walls
 
Modular Java - OSGi
Modular Java - OSGiModular Java - OSGi
Modular Java - OSGiCraig Walls
 
Java Configuration Deep Dive with Spring
Java Configuration Deep Dive with SpringJava Configuration Deep Dive with Spring
Java Configuration Deep Dive with SpringJoshua Long
 
Level 3 REST Makes Your API Browsable
Level 3 REST Makes Your API BrowsableLevel 3 REST Makes Your API Browsable
Level 3 REST Makes Your API BrowsableMatt Bishop
 
Spring Boot in Action
Spring Boot in Action Spring Boot in Action
Spring Boot in Action Alex Movila
 
Spring MVC - Web Forms
Spring MVC  - Web FormsSpring MVC  - Web Forms
Spring MVC - Web FormsIlio Catallo
 
Testing Spring MVC and REST Web Applications
Testing Spring MVC and REST Web ApplicationsTesting Spring MVC and REST Web Applications
Testing Spring MVC and REST Web ApplicationsSam Brannen
 
REST Enabling Your Oracle Database
REST Enabling Your Oracle DatabaseREST Enabling Your Oracle Database
REST Enabling Your Oracle DatabaseJeff Smith
 
Make Your API Irresistible
Make Your API IrresistibleMake Your API Irresistible
Make Your API Irresistibleduvander
 
Soap and restful webservice
Soap and restful webserviceSoap and restful webservice
Soap and restful webserviceDong Ngoc
 
Struts 2 + Spring
Struts 2 + SpringStruts 2 + Spring
Struts 2 + SpringBryan Hsueh
 

Viewers also liked (20)

RESTful Web Services with Spring MVC
RESTful Web Services with Spring MVCRESTful Web Services with Spring MVC
RESTful Web Services with Spring MVC
 
Spring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSpring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. REST
 
Microservices with Spring Boot
Microservices with Spring BootMicroservices with Spring Boot
Microservices with Spring Boot
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
 
Java 8 - New Features
Java 8 - New FeaturesJava 8 - New Features
Java 8 - New Features
 
That old Spring magic has me in its SpEL
That old Spring magic has me in its SpELThat old Spring magic has me in its SpEL
That old Spring magic has me in its SpEL
 
Modular Java - OSGi
Modular Java - OSGiModular Java - OSGi
Modular Java - OSGi
 
Java Configuration Deep Dive with Spring
Java Configuration Deep Dive with SpringJava Configuration Deep Dive with Spring
Java Configuration Deep Dive with Spring
 
Boot It Up
Boot It UpBoot It Up
Boot It Up
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
 
Level 3 REST Makes Your API Browsable
Level 3 REST Makes Your API BrowsableLevel 3 REST Makes Your API Browsable
Level 3 REST Makes Your API Browsable
 
Spring Boot in Action
Spring Boot in Action Spring Boot in Action
Spring Boot in Action
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
 
Spring MVC - Web Forms
Spring MVC  - Web FormsSpring MVC  - Web Forms
Spring MVC - Web Forms
 
Testing Spring MVC and REST Web Applications
Testing Spring MVC and REST Web ApplicationsTesting Spring MVC and REST Web Applications
Testing Spring MVC and REST Web Applications
 
REST Enabling Your Oracle Database
REST Enabling Your Oracle DatabaseREST Enabling Your Oracle Database
REST Enabling Your Oracle Database
 
Make Your API Irresistible
Make Your API IrresistibleMake Your API Irresistible
Make Your API Irresistible
 
Soap and restful webservice
Soap and restful webserviceSoap and restful webservice
Soap and restful webservice
 
Struts 2 + Spring
Struts 2 + SpringStruts 2 + Spring
Struts 2 + Spring
 
Springboot Overview
Springboot  OverviewSpringboot  Overview
Springboot Overview
 

Similar to Building RESTful applications using Spring MVC

Rest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.jsRest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.jsCarol McDonald
 
Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web servicesnbuddharaju
 
JAX-RS 2.0 and OData
JAX-RS 2.0 and ODataJAX-RS 2.0 and OData
JAX-RS 2.0 and ODataAnil Allewar
 
May 2010 - RestEasy
May 2010 - RestEasyMay 2010 - RestEasy
May 2010 - RestEasyJBug Italy
 
Rest presentation
Rest  presentationRest  presentation
Rest presentationsrividhyau
 
Spring 3.x - Spring MVC - Advanced topics
Spring 3.x - Spring MVC - Advanced topicsSpring 3.x - Spring MVC - Advanced topics
Spring 3.x - Spring MVC - Advanced topicsGuy Nir
 
Introduction to the SharePoint Client Object Model and REST API
Introduction to the SharePoint Client Object Model and REST APIIntroduction to the SharePoint Client Object Model and REST API
Introduction to the SharePoint Client Object Model and REST APIRob Windsor
 
Multi Client Development with Spring for SpringOne 2GX 2013 with Roy Clarkson
Multi Client Development with Spring for SpringOne 2GX 2013 with Roy ClarksonMulti Client Development with Spring for SpringOne 2GX 2013 with Roy Clarkson
Multi Client Development with Spring for SpringOne 2GX 2013 with Roy ClarksonJoshua Long
 
RESTful Web Services with JAX-RS
RESTful Web Services with JAX-RSRESTful Web Services with JAX-RS
RESTful Web Services with JAX-RSCarol McDonald
 
Ppt on web development and this has all details
Ppt on web development and this has all detailsPpt on web development and this has all details
Ppt on web development and this has all detailsgogijoshiajmer
 
CDI, Seam & RESTEasy: You haven't seen REST yet!
CDI, Seam & RESTEasy: You haven't seen REST yet!CDI, Seam & RESTEasy: You haven't seen REST yet!
CDI, Seam & RESTEasy: You haven't seen REST yet!Dan Allen
 
Spark IT 2011 - Developing RESTful Web services with JAX-RS
Spark IT 2011 - Developing RESTful Web services with JAX-RSSpark IT 2011 - Developing RESTful Web services with JAX-RS
Spark IT 2011 - Developing RESTful Web services with JAX-RSArun Gupta
 
Services in Drupal 8
Services in Drupal 8Services in Drupal 8
Services in Drupal 8Andrei Jechiu
 
Network Device Database Management with REST using Jersey
Network Device Database Management with REST using JerseyNetwork Device Database Management with REST using Jersey
Network Device Database Management with REST using JerseyPayal Jain
 

Similar to Building RESTful applications using Spring MVC (20)

Rest
RestRest
Rest
 
Rest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.jsRest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.js
 
Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web services
 
JAX-RS 2.0 and OData
JAX-RS 2.0 and ODataJAX-RS 2.0 and OData
JAX-RS 2.0 and OData
 
RESTEasy
RESTEasyRESTEasy
RESTEasy
 
Services Stanford 2012
Services Stanford 2012Services Stanford 2012
Services Stanford 2012
 
May 2010 - RestEasy
May 2010 - RestEasyMay 2010 - RestEasy
May 2010 - RestEasy
 
Day7
Day7Day7
Day7
 
Rest presentation
Rest  presentationRest  presentation
Rest presentation
 
Spring 3.x - Spring MVC - Advanced topics
Spring 3.x - Spring MVC - Advanced topicsSpring 3.x - Spring MVC - Advanced topics
Spring 3.x - Spring MVC - Advanced topics
 
RESTing with JAX-RS
RESTing with JAX-RSRESTing with JAX-RS
RESTing with JAX-RS
 
Introduction to the SharePoint Client Object Model and REST API
Introduction to the SharePoint Client Object Model and REST APIIntroduction to the SharePoint Client Object Model and REST API
Introduction to the SharePoint Client Object Model and REST API
 
Multi Client Development with Spring for SpringOne 2GX 2013 with Roy Clarkson
Multi Client Development with Spring for SpringOne 2GX 2013 with Roy ClarksonMulti Client Development with Spring for SpringOne 2GX 2013 with Roy Clarkson
Multi Client Development with Spring for SpringOne 2GX 2013 with Roy Clarkson
 
RESTful Web Services with JAX-RS
RESTful Web Services with JAX-RSRESTful Web Services with JAX-RS
RESTful Web Services with JAX-RS
 
Rest with Spring
Rest with SpringRest with Spring
Rest with Spring
 
Ppt on web development and this has all details
Ppt on web development and this has all detailsPpt on web development and this has all details
Ppt on web development and this has all details
 
CDI, Seam & RESTEasy: You haven't seen REST yet!
CDI, Seam & RESTEasy: You haven't seen REST yet!CDI, Seam & RESTEasy: You haven't seen REST yet!
CDI, Seam & RESTEasy: You haven't seen REST yet!
 
Spark IT 2011 - Developing RESTful Web services with JAX-RS
Spark IT 2011 - Developing RESTful Web services with JAX-RSSpark IT 2011 - Developing RESTful Web services with JAX-RS
Spark IT 2011 - Developing RESTful Web services with JAX-RS
 
Services in Drupal 8
Services in Drupal 8Services in Drupal 8
Services in Drupal 8
 
Network Device Database Management with REST using Jersey
Network Device Database Management with REST using JerseyNetwork Device Database Management with REST using Jersey
Network Device Database Management with REST using Jersey
 

More from IndicThreads

Http2 is here! And why the web needs it
Http2 is here! And why the web needs itHttp2 is here! And why the web needs it
Http2 is here! And why the web needs itIndicThreads
 
Understanding Bitcoin (Blockchain) and its Potential for Disruptive Applications
Understanding Bitcoin (Blockchain) and its Potential for Disruptive ApplicationsUnderstanding Bitcoin (Blockchain) and its Potential for Disruptive Applications
Understanding Bitcoin (Blockchain) and its Potential for Disruptive ApplicationsIndicThreads
 
Go Programming Language - Learning The Go Lang way
Go Programming Language - Learning The Go Lang wayGo Programming Language - Learning The Go Lang way
Go Programming Language - Learning The Go Lang wayIndicThreads
 
Building Resilient Microservices
Building Resilient Microservices Building Resilient Microservices
Building Resilient Microservices IndicThreads
 
App using golang indicthreads
App using golang  indicthreadsApp using golang  indicthreads
App using golang indicthreadsIndicThreads
 
Building on quicksand microservices indicthreads
Building on quicksand microservices  indicthreadsBuilding on quicksand microservices  indicthreads
Building on quicksand microservices indicthreadsIndicThreads
 
How to Think in RxJava Before Reacting
How to Think in RxJava Before ReactingHow to Think in RxJava Before Reacting
How to Think in RxJava Before ReactingIndicThreads
 
Iot secure connected devices indicthreads
Iot secure connected devices indicthreadsIot secure connected devices indicthreads
Iot secure connected devices indicthreadsIndicThreads
 
Real world IoT for enterprises
Real world IoT for enterprisesReal world IoT for enterprises
Real world IoT for enterprisesIndicThreads
 
IoT testing and quality assurance indicthreads
IoT testing and quality assurance indicthreadsIoT testing and quality assurance indicthreads
IoT testing and quality assurance indicthreadsIndicThreads
 
Functional Programming Past Present Future
Functional Programming Past Present FutureFunctional Programming Past Present Future
Functional Programming Past Present FutureIndicThreads
 
Harnessing the Power of Java 8 Streams
Harnessing the Power of Java 8 Streams Harnessing the Power of Java 8 Streams
Harnessing the Power of Java 8 Streams IndicThreads
 
Building & scaling a live streaming mobile platform - Gr8 road to fame
Building & scaling a live streaming mobile platform - Gr8 road to fameBuilding & scaling a live streaming mobile platform - Gr8 road to fame
Building & scaling a live streaming mobile platform - Gr8 road to fameIndicThreads
 
Internet of things architecture perspective - IndicThreads Conference
Internet of things architecture perspective - IndicThreads ConferenceInternet of things architecture perspective - IndicThreads Conference
Internet of things architecture perspective - IndicThreads ConferenceIndicThreads
 
Cars and Computers: Building a Java Carputer
 Cars and Computers: Building a Java Carputer Cars and Computers: Building a Java Carputer
Cars and Computers: Building a Java CarputerIndicThreads
 
Scrap Your MapReduce - Apache Spark
 Scrap Your MapReduce - Apache Spark Scrap Your MapReduce - Apache Spark
Scrap Your MapReduce - Apache SparkIndicThreads
 
Continuous Integration (CI) and Continuous Delivery (CD) using Jenkins & Docker
 Continuous Integration (CI) and Continuous Delivery (CD) using Jenkins & Docker Continuous Integration (CI) and Continuous Delivery (CD) using Jenkins & Docker
Continuous Integration (CI) and Continuous Delivery (CD) using Jenkins & DockerIndicThreads
 
Speed up your build pipeline for faster feedback
Speed up your build pipeline for faster feedbackSpeed up your build pipeline for faster feedback
Speed up your build pipeline for faster feedbackIndicThreads
 
Unraveling OpenStack Clouds
 Unraveling OpenStack Clouds Unraveling OpenStack Clouds
Unraveling OpenStack CloudsIndicThreads
 
Digital Transformation of the Enterprise. What IT leaders need to know!
Digital Transformation of the Enterprise. What IT  leaders need to know!Digital Transformation of the Enterprise. What IT  leaders need to know!
Digital Transformation of the Enterprise. What IT leaders need to know!IndicThreads
 

More from IndicThreads (20)

Http2 is here! And why the web needs it
Http2 is here! And why the web needs itHttp2 is here! And why the web needs it
Http2 is here! And why the web needs it
 
Understanding Bitcoin (Blockchain) and its Potential for Disruptive Applications
Understanding Bitcoin (Blockchain) and its Potential for Disruptive ApplicationsUnderstanding Bitcoin (Blockchain) and its Potential for Disruptive Applications
Understanding Bitcoin (Blockchain) and its Potential for Disruptive Applications
 
Go Programming Language - Learning The Go Lang way
Go Programming Language - Learning The Go Lang wayGo Programming Language - Learning The Go Lang way
Go Programming Language - Learning The Go Lang way
 
Building Resilient Microservices
Building Resilient Microservices Building Resilient Microservices
Building Resilient Microservices
 
App using golang indicthreads
App using golang  indicthreadsApp using golang  indicthreads
App using golang indicthreads
 
Building on quicksand microservices indicthreads
Building on quicksand microservices  indicthreadsBuilding on quicksand microservices  indicthreads
Building on quicksand microservices indicthreads
 
How to Think in RxJava Before Reacting
How to Think in RxJava Before ReactingHow to Think in RxJava Before Reacting
How to Think in RxJava Before Reacting
 
Iot secure connected devices indicthreads
Iot secure connected devices indicthreadsIot secure connected devices indicthreads
Iot secure connected devices indicthreads
 
Real world IoT for enterprises
Real world IoT for enterprisesReal world IoT for enterprises
Real world IoT for enterprises
 
IoT testing and quality assurance indicthreads
IoT testing and quality assurance indicthreadsIoT testing and quality assurance indicthreads
IoT testing and quality assurance indicthreads
 
Functional Programming Past Present Future
Functional Programming Past Present FutureFunctional Programming Past Present Future
Functional Programming Past Present Future
 
Harnessing the Power of Java 8 Streams
Harnessing the Power of Java 8 Streams Harnessing the Power of Java 8 Streams
Harnessing the Power of Java 8 Streams
 
Building & scaling a live streaming mobile platform - Gr8 road to fame
Building & scaling a live streaming mobile platform - Gr8 road to fameBuilding & scaling a live streaming mobile platform - Gr8 road to fame
Building & scaling a live streaming mobile platform - Gr8 road to fame
 
Internet of things architecture perspective - IndicThreads Conference
Internet of things architecture perspective - IndicThreads ConferenceInternet of things architecture perspective - IndicThreads Conference
Internet of things architecture perspective - IndicThreads Conference
 
Cars and Computers: Building a Java Carputer
 Cars and Computers: Building a Java Carputer Cars and Computers: Building a Java Carputer
Cars and Computers: Building a Java Carputer
 
Scrap Your MapReduce - Apache Spark
 Scrap Your MapReduce - Apache Spark Scrap Your MapReduce - Apache Spark
Scrap Your MapReduce - Apache Spark
 
Continuous Integration (CI) and Continuous Delivery (CD) using Jenkins & Docker
 Continuous Integration (CI) and Continuous Delivery (CD) using Jenkins & Docker Continuous Integration (CI) and Continuous Delivery (CD) using Jenkins & Docker
Continuous Integration (CI) and Continuous Delivery (CD) using Jenkins & Docker
 
Speed up your build pipeline for faster feedback
Speed up your build pipeline for faster feedbackSpeed up your build pipeline for faster feedback
Speed up your build pipeline for faster feedback
 
Unraveling OpenStack Clouds
 Unraveling OpenStack Clouds Unraveling OpenStack Clouds
Unraveling OpenStack Clouds
 
Digital Transformation of the Enterprise. What IT leaders need to know!
Digital Transformation of the Enterprise. What IT  leaders need to know!Digital Transformation of the Enterprise. What IT  leaders need to know!
Digital Transformation of the Enterprise. What IT leaders need to know!
 

Recently uploaded

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 

Recently uploaded (20)

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

Building RESTful applications using Spring MVC

  • 1. Restful applications using Spring MVC Kamal Govindraj TenXperts Technologies
  • 2. About Me  Programming for 13 Years  Architect @ TenXperts Technologies  Trainer / Consultant @ SpringPeople Technologies  Enteprise applications leveraging open source frameworks (spring,hibernate, gwt,jbpm..)  Key contributor to InfraRED & Grails jBPM plugin (open source)
  • 3. Agenda  What is REST?  REST Concepts  RESTful Architecture Design  Spring @MVC  Implement REST api using Spring @MVC 3.0
  • 4. What is REST?  Representational State Transfer  Term coined up by Roy Fielding − Author of HTTP spec  Architectural style  Architectural basis for HTTP − Defined a posteriori
  • 5. Core REST Concepts  Identifiable Resources  Uniform interface  Stateless conversation  Resource representations  Hypermedia
  • 6. Identifiable Resources  Everything is a resource − Customer − Order − Catalog Item  Resources are accessed by URIs
  • 7. Uniform interface  Interact with Resource using single, fixed interface  GET /orders - fetch list of orders  GET /orders/1 - fetch order with id 1  POST /orders – create new order  PUT /orders/1 – update order with id 1  DELETE /orders/1 – delete order with id 1  GET /customers/1/order – all orders for customer with id 1
  • 8. Resource representations  More that one representation possible − text/html, image/gif, application/pdf  Desired representation set in Accept HTTP header − Or file extension  Delivered representation show in Content- Type  Access resources through representation  Prefer well-known media types
  • 9. Stateless conversation  Server does not maintain state − Don’t use the Session!  Client maintains state through links  Very scalable  Enforces loose coupling (no shared session knowledge)
  • 10. Hypermedia  Resources contain links  Client state transitions are made through these links  Links are provided by server  Example <oder ref=”/order/1”> <customer ref=”/customers/1”/> <lineItems> <lineItem item=”/catalog/item/125” qty=”10”/> <lineItem item=”/catalog/item/150” qty=”5”> </lineItems> </oder>
  • 12. RESTful application design  Identify resources − Design URIs  Select representations − Or create new ones  Identify method semantics  Select response codes
  • 13. Implement Restful API using Spring MVC
  • 14. Spring @MVC @Controller public class AccountController { @Autowired AccountService accountService; @RequestMapping("/details") public String details(@RequestParam("id")Long id, Model model) { Account account = accountService.findAccount(id); model.addAttribute(account); return "account/details"; } } account/details.jsp <h1>Account Details</h1> <p>Name : ${account.name}</p> <p>Balance : ${account.balance}</p> ../details?id=1
  • 15. RESTful support in Spring 3.0  Extends Spring @MVC to handle URL templates - @PathVariable  Leverages Spring OXM module to provide marshalling / unmarshalling (castor, xstream, jaxb etc)  @RequestBody – extract request body to method parameter  @ResponseBody – render return value to response body using converter – no views required
  • 16. REST controller @Controller public class AccountController { @RequestMapping(value="/accounts",method=RequestMethod.GET) @ResponseBody public AccountList getAllAcccount() { } @RequestMapping(value="/accounts/${id}",method=RequestMethod.GET) @ResponseBody public Account getAcccount(@PathVariable("id")Long id) { } @RequestMapping(value="/accounts/",method=RequestMethod.POST) @ResponseBody public Long createAcccount(@RequestBody Account account) { } @RequestMapping(value="/accounts/${id}",method=RequestMethod.PUT) @ResponseBody public Account updateAcccount(@PathVariable("id")Long id, @RequestBody Account account) { } @RequestMapping(value="/accounts/${id}",method=RequestMethod.DELETE) @ResponseBody public void deleteAcccount(@PathVariable("id")Long id) { } }
  • 17. web.xml <servlet> <servlet-name>rest-api</servlet-name> <servlet-class>org....web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>rest-api</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping>
  • 18. rest-api-servlet-context.xml <bean id="marshaller" class="...oxm.xstream.XStreamMarshaller"> <property name="aliases"> <map> <entry key="category" value="...domain.Category"/> <entry key="catalogItem" value="...domain.CatalogItem"/> </map> </property> </bean>
  • 19. rest-api-servlet-context.xml <bean class="...mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <util:list> <bean class="...http.converter.xml.MarshallingHttpMessageConverter"> <constructor-arg ref="marshaller" /> </bean> <bean class="...http.converter.StringHttpMessageConverter" /> </util:list> </property> </bean>
  • 20. Invoke REST services  The new RestTemplate class provides client- side invocation of a RESTful web-service HTTP RestTemplate Method Method DELETE delete(String url, String... urlVariables) GET getForObject(String url, Class<t> responseType, String … urlVariables) HEAD headForHeaders(String url, String… urlVariables) OPTIONS optionsForAllow(String url, String… urlVariables) POST postForLocation(String url, Object request, String… urlVariables) PUT put(String url, Object request, String…urlVariables)
  • 21. REST template example AccountList accounts = restTemplate.getForObject("/accounts/",AccountList.class); Account account = restTemplate.getForObject("/accounts/${id}",Account.class ,"1"); restTemplate.postForLocation("/accounts/", account); restTemplate.put("/accounts/${id}",account,"1"); restTemplate.delete("/accounts/${id}", "1");
  • 22. Demo