cisc 3120 c25: creating web services using the …...cisc 3120 c25: creating web services using the...

35
CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn College 5/9/2018 1 CUNY | Brooklyn College

Upload: others

Post on 22-May-2020

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

CISC 3120

C25: Creating Web Services using the Spring Framework

Hui Chen

Department of Computer & Information Science

CUNY Brooklyn College

5/9/2018 1CUNY | Brooklyn College

Page 2: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Outline

• Recap & Review

• The Model-View-Controller (MVC) pattern

• MVC on the Web and evolution

• Spring MVC

• Evolving Spring MVC and Web Services

• REST and RESTful Web Services

• Implementing RESTful Web Services

• Query and update (read and write)

5/9/2018 CUNY | Brooklyn College 2

Page 3: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Web Application Architecture

5/9/2018 CUNY | Brooklyn College 3

• 3-tier (and n-tier)

Web Application Client

Web and Application Servers

Data Storage (Database Server)

Client sideapps

Application Server

&Server-

side Apps

Data store (e.g.,

relational & non-relational

databases and stored procedures)

HTTP ……

Page 4: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Model-View-Controller

5/9/2018 CUNY | Brooklyn College 4

Model

Controller

View

Page 5: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Apply MVC to the Web?

• Where are they?

5/9/2018 CUNY | Brooklyn College 5

Web Application Client

Web and Application Servers

Data Storage (Database Server)

Client sideapps

Application Server

&Server-

side Apps

Data store (e.g.,

relational & non-relational

databases and stored procedures)

HTTP ……

Page 6: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Update and Render View

• Update view at the server & render view at the client

5/9/2018 CUNY | Brooklyn College 6

Web Client Web and Application Server Storage System (Database Server)

HTTP …

ModelController

View(update view)

(render view)

Page 7: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Update and Render View

• Update and render view at the client

5/9/2018 CUNY | Brooklyn College 7

Web Client Web and Application Server

Storage System (Database Server)

HTTP …

Model

Controller

View(update andrender view)

Page 8: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Update and Render View: MVC and MVC …

• Update and render view at the client

5/9/2018 CUNY | Brooklyn College 8

Web Client Web and Application Server

Storage System (Database Server)

HTTP

Model

ControllerUser AgentController

User AgentModel

User AgentView

Page 9: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Web MVC: Evolution

• Initially, update view at the server

• Constant network connectivity

• Wired connectivity, stationary stations

• Few interactions, tolerate high latency

• Page-based update, synchronous request-response

• Evolve to update view at the client

• Intermittent network connectivity

• Wireless connectivity, mobile stations

• Many interactions, expect low latency

• Component-based update, asynchronous request-response

• Often, RESTful web services & feature-rich client

5/9/2018 CUNY | Brooklyn College 9

Page 10: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

REST

• Representation State Transfer

• Architecture style for networked-base applications

• Define a set of constraints

• Commonly used in the Web applications

5/9/2018 CUNY | Brooklyn College 10

Page 11: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

REST Constraints

• All important resources are identified by one resource identifier mechanism

• e.g., URI

• “everything is a resource on the web”

• Access methods have the same semantics for all resources

• Resources are manipulated through the exchange of representations, e.g., URI

• Representations are exchanged via self-descriptive messages, e.g., HTTP messages

• Hypertext as the engine of application state

• HTTP is stateless

• Maintaining state via hypertext messages

5/9/2018 CUNY | Brooklyn College 11

Page 12: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Questions

• Apply MVC to the Web?

• Evolution of the Web

• Feature rich Web applications

• REST and RESTful Web services

5/9/2018 CUNY | Brooklyn College 12

Page 13: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Design with Spring Controller: User Agent for Rendering Views

• @Controller annotate a Web Controller

5/9/2018 CUNY | Brooklyn College 13

Web Client Web and Application ServerStorage System (Database Server)

HTTPRequest

Model@Controller

View(update view, e.g., via page

template, JSP)

(render view)HTTP

Response

HTML documents

Page 14: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Design with Spring Controller: User Agent Processing Data

• @Controller annotates a Web Controller, @ResponseBody annotates response type

5/9/2018 CUNY | Brooklyn College 14

Web Client Web and Application Server Storage System (Database Server)

HTTP …

Model@RestController

Data needed for View

(Json/XML)

User Agent MVC

Process data & render

view

Page 15: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Design with Spring Controller: REST and User Agent

• @RestController combines @Controller and @ResponseBody

5/9/2018 CUNY | Brooklyn College 15

Web Client Web and Application Server Storage System (Database Server)

HTTP …

Model@RestController

Data needed for View

(Json/XML)

User Agent MVC

Process data & render

view

Page 16: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Approaches in the Spring Framework

• Spring WebMVC

• The original Spring Web framework built on the Servlet API from inception of the Spring framework.

• Spring WebFlux

• The new Spring Web framework introduced in the Spring Framework 5.0

5/9/2018 CUNY | Brooklyn College 16

Page 17: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

The Spring Framework version 5

• The Spring Framework is evolving as the Web development.

5/9/2018 CUNY | Brooklyn College 17

Page 18: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Spring WebMVC

• A MVC implementation on the Web

• A central controller that processes and dispatches all HTTP requests

• Problem

• Synchronous (Filter and Servlet)

• Blocking (getParameter and getPart methods)

5/9/2018 CUNY | Brooklyn College 18

Filter, Servlet

Page 19: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Spring WebFlux

• Support a non-blocking web stack

• Handle concurrency with a small number of threads

• Scale with less hardware resources

5/9/2018 CUNY | Brooklyn College 19

Page 20: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Reactive Spring Web

• WebFlux support reactive programming

• Applications are built around reacting to change, e.g.,

• Network component reacting to I/O events

• UI controller reacting to mouse events.

• Non-blocking is reactive

• Reacting to notifications as operations complete or data becomes available

5/9/2018 CUNY | Brooklyn College 20

Page 21: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Questions

• The Spring Framework

• Overview of creating RESTful services in the Spring Framework

5/9/2018 CUNY | Brooklyn College 21

Page 22: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Query and Update

• Two primary types of operations

• Query

• Retrieve data from the Web services based on the input from the user agent

• Update

• Update data at the Web services using the input from the user agent

5/9/2018 CUNY | Brooklyn College 22

Page 23: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Persistent Data Store

• Necessary to store data

5/9/2018 CUNY | Brooklyn College 23

Web Application Client

Web and Application Servers

Data Storage (Database Server)

Client sideapps

Application Server

&Server-

side Apps

Data store (e.g.,

relational & non-relational

databases and stored procedures)

HTTP ……

Page 24: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Basic Data Store: CRUD

• Basic functions of persistent storage

• Create, read, update, and delete

• Map to HTTP requests

• Examples

• PUT /addresses/1

• GET /addresses/1

• POST /addresses

• DELETE /addresses/1

5/9/2018 CUNY | Brooklyn College 24

Page 25: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Example Data Store: MongoDB

• Document-like persistent storage

• Basic operations based key-value pairs

5/9/2018 CUNY | Brooklyn College 25

Page 26: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Example with Persistent Data Store

• Passing little data (e.g., primitive values or Strings)

• Passing lots of data (e.g., an object)

5/9/2018 CUNY | Brooklyn College 26

26

Web server

HTTP

JSON/XML objects(query & update: difference

here?)

Path variables & request parameters

query

update

Page 27: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Questions

• Persistent data stores

• Examples

• Passing small amount of data

• Passing lots of data

• Query and update (read and write)

5/9/2018 CUNY | Brooklyn College 27

Page 28: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Example Application with Persistent Data Store

• RESTful Web service using Spring Framework

• MongoDB for data storage

5/9/2018 CUNY | Brooklyn College 28

Page 29: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Example: GpaMongoWebService

• pom.xml

• Examine the dependencies

• Application configuration

• application.properties (where is it?)

• Port number, SSL certificate, and database

• GpaMongoWebService

• Created by the Spring Suite Tools

• Mongo repositories

• GpaController

• Mapping between Web APIs and methods

5/9/2018 CUNY | Brooklyn College 29

Page 30: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Example: GpaMongoWebService: Persistent Data Store

• Package edu.cuny.brooklyn.web.data

• Built upon Spring’s CRUD repositories

• Queries implementations are automatically created by the Spring framework

• However the method signatures must conform with the Spring convention, e.g.,

• findBy…, findAll..., findAllBy…

• A few Spring Framework interfaces

• CrudRepository, PagingAndSortingRepository, MongoRepository, SimpleMongoRepository

5/9/2018 CUNY | Brooklyn College 30

Page 31: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Example: GpaMongoWebService: Error Handling

• Package edu.cuny.brooklyn.web.exception

• Return error message in JSON

5/9/2018 CUNY | Brooklyn College 31

Page 32: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Example: GpaMongoWebService: Model and Data

• Package edu.cuny.brooklyn.web.service

• Package edu.cuny.brooklyn.web.data

5/9/2018 CUNY | Brooklyn College 32

Page 33: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Eye-Ball Testing

• Testing without implementing a User Agent

• Designing and implementing a User Agent can be time consuming

• Recommend cURL

• Sample scripts provided for both Windows and OSX/Linux

• Run scripts from command line

• Create scripts by revising scripts for your own work

• API

• data

5/9/2018 CUNY | Brooklyn College 33

Page 34: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Further Reading

• Roy T. Fielding, Richard N. Taylor, Justin R. Erenkrantz, Michael M. Gorlick, Jim Whitehead, Rohit Khare, and Peyman Oreizy. 2017. Reflections on the REST architectural style and "principled design of the modern web architecture" In Proceedings of the 2017 11th Joint Meeting on Foundations of Software Engineering (ESEC/FSE 2017). ACM, New York, NY, USA, 4-14. DOI: https://doi.org/10.1145/3106237.3121282

• Spring Framework Documentation & Guide

• “Understanding REST”

• “Building a RESTful Web Service”

• “Serving Web Content with Spring MVC”

• “Reactive Programming with Spring 5.0 M1”

• “Notes on Reactive Program Part I and Part II”

5/9/2018 CUNY | Brooklyn College 34

Page 35: CISC 3120 C25: Creating Web Services using the …...CISC 3120 C25: Creating Web Services using the Spring Framework Hui Chen Department of Computer & Information Science CUNY Brooklyn

Questions

• Understand the big picture

• Create Web applications in Java

• Example Web services design and implementation

• Eye-ball testing

5/9/2018 CUNY | Brooklyn College 35