Chapter 4C REST wz Jackson
Chapter 4C REST wz Jackson
Fig: DispatcherServlet
Questions
m
You will learn how to …
• Create REST APIs / Web Services with Spring
You will learn how to …
• Create REST APIs / Web Services with Spring
• Not an A to Z reference … for that you can see Spring Reference Manual
Practical Results
• Not an A to Z reference … for that you can see Spring Reference Manual
https://projects.spring.io/spring-
framework/
Business Problem
Business Problem
• Build a client app that provides the weather report for a city
Business Problem
• Build a client app that provides the weather report for a city
My
Weather
App
Application Architecture
My
Weather
App
My Weather
Weather Service
App (external)
My Weather
Weather Service
App (external)
City
My Weather
Weather Service
App (external)
City
My Weather
Weather Service
App (external)
Weather Report
Java JavaScript
C# Go
Swift
PHP Ruby
Python
etc…
Answers
Answers
• What is the data format?
Answers
• What is the data format?
api.openweathermap.org/data/2.5/weather?q={city name}
OR
{
...
"temp": 14,
"temp_min": 11,
"temp_max": 17,
"humidity": 81,
"name": "London",
... Condensed
} version
Multiple Client Apps
Multiple Client Apps
Weather
Service
(external)
Multiple Client Apps
My
Weather
Spring MVC
Weather
Service
(external)
Multiple Client Apps
My
Weather
Spring MVC
My Weather
Weather Service
C# App
(external)
Multiple Client Apps
My
Weather
Spring MVC
My Weather
Weather Service
C# App
(external)
My
Weather
iPhone App
Multiple Client Apps Remember:
My
Weather
Spring MVC
My Weather
Weather Service
C# App
(external)
My
Weather
iPhone App
Multiple Client Apps Remember:
REST calls can be made over HTTP
My
Weather
Spring MVC
My Weather
Weather Service
C# App
(external)
My
Weather
iPhone App
Multiple Client Apps Remember:
REST calls can be made over HTTP
REST is language independent
My
Weather
Spring MVC
My Weather
Weather Service
C# App
(external)
My
Weather
iPhone App
Currency Converter App
Currency Converter App
My
Currency
App
Currency Converter App
My Currency
Currency Service
App (external)
Currency Converter App
Of course this
fluctuates based
on the market
Movie TicketsApp
Movie Tickets App
My
Movie
App
Movie TicketsApp
My Movie Tickets
Movie Service
App (external)
Movie TicketsApp
CRM App
Customer Relationship Manager (CRM) App
CRM
CRM App Service
(spring-rest)
Customer Relationship Manager (CRM) App
Get customers
CRM
CRM App Service
(spring-rest)
Customer list
Customer Relationship Manager (CRM) App
Get customers
CRM
CRM App Service
(spring-rest)
Customer list
We will create
this code using
Spring REST
(SERVER)
Customer Relationship Manager (CRM) App
Get customers
CRM
CRM App Service
(spring-rest)
Customer list
Where to Find REST APIs
www.programmableweb.com
What do we call it?
What do we call it?
REST API
RESTful API
What do we call it?
REST
REST API
Web Services
RESTful
RESTful API
Web Services
What do we call it?
REST
REST API REST Services
Web Services
RESTful
RESTful API RESTful Services
Web Services
What do we call it?
REST
REST API REST Services
Web Services
RESTful
RESTful API RESTful Services
Web Services
REST
REST API REST Services
Web Services
RESTful
RESTful API RESTful Services
Web Services
m
What is JSON?
What is JSON?
• Lightweight data format for storing and exchanging data … plain text
What is JSON?
• Lightweight data format for storing and exchanging data … plain text
• Lightweight data format for storing and exchanging data … plain text
• Can use with any programming language: Java, C#, Python etc …
What is JSON?
• Lightweight data format for storing and exchanging data … plain text
JSON is just
• Language independent … not just for JavaScript plain text
data
• Can use with any programming language: Java, C#, Python etc …
Simple JSON Example
Simple JSON Example
{
"id": 14,
"firstName": "Mario",
"lastName": "Rossi",
"active": true
}
Simple JSON Example
{
"id": 14,
"firstName": "Mario",
"lastName": "Rossi",
"active": true
}
Simple JSON Example
{
"id": 14,
• Object members are name / value pairs
"firstName": "Mario",
"lastName": "Rossi",
"active": true
}
Simple JSON Example
{
"id": 14,
• Object members are name / value pairs
"firstName": "Mario",
"lastName": "Rossi",
• Delimited by colons "active": true
}
Simple JSON Example
Name Value
• Curley braces define objects in JSON
{
"id": 14,
• Object members are name / value pairs
"firstName": "Mario",
"lastName": "Rossi",
• Delimited by colons "active": true
}
Simple JSON Example
Name Value
• Curley braces define objects in JSON
{
"id": 14,
• Object members are name / value pairs
"firstName": "Mario",
"lastName": "Rossi",
• Delimited by colons "active": true
}
{
"id": 14,
"firstName": "Mario",
"lastName": "Rossi",
"active": true,
"courses" : null
}
JSON Values
Value
{
"id": 14,
"firstName": "Mario",
"lastName": "Rossi",
"active": true,
"courses" : null
}
JSON Values
Value
• Numbers: no quotes
{
"id": 14,
"firstName": "Mario",
"lastName": "Rossi",
"active": true,
"courses" : null
}
JSON Values
Value
• Numbers: no quotes
{
• String: in double quotes "id": 14,
"firstName": "Mario",
"lastName": "Rossi",
"active": true,
"courses" : null
}
JSON Values
Value
• Numbers: no quotes
{
• String: in double quotes "id": 14,
"firstName": "Mario",
• Boolean: true, false "lastName": "Rossi",
"active": true,
"courses" : null
}
JSON Values
Value
• Numbers: no quotes
{
• String: in double quotes "id": 14,
"firstName": "Mario",
• Boolean: true, false "lastName": "Rossi",
"active": true,
• Nested JSON object "courses" : null
}
JSON Values
Value
• Numbers: no quotes
{
• String: in double quotes "id": 14,
"firstName": "Mario",
• Boolean: true, false "lastName": "Rossi",
"active": true,
• Nested JSON object "courses" : null
}
• Array
JSON Values
• Numbers: no Value
quotes
• String: in {
"id": 14,
double
"firstName": "Mario",
quotes "lastName": "Rossi",
"active": true,
• Boolean: "courses" : null
true, false }
• Nested JSON
object
• Array
• null
Nested JSON Objects
Nested JSON Objects
{
"id": 14,
"firstName": "Mario",
"lastName": "Rossi",
"active": true,
"address" : {
"street" : "100 Main St",
"city" : "Philadelphia",
"state" : "Pennsylvania",
"zip" : "19103",
"country" : "USA"
}
}
Nested JSON Objects
{
"id": 14,
"firstName": "Mario", Nested
"lastName": "Rossi",
"active": true,
"address" : {
"street" : "100 Main St",
"city" : "Philadelphia",
"state" : "Pennsylvania",
"zip" : "19103",
"country" : "USA"
}
}
JSON Arrays
JSON Arrays
{
"id": 14,
"firstName": "Mario",
"lastName": "Rossi",
"active": true,
"languages" : ["Java", "C#", "Python", "Javascript"]
}
JSON Arrays
{
"id": 14, Array
"firstName": "Mario",
"lastName": "Rossi",
"active": true,
"languages" : ["Java", "C#", "Python", "Javascript"]
}
Java JSON Data Binding
m
Java JSON Data Binding
Java JSON Data Binding
JSON
Java JSON Data Binding
Java
JSON
POJO
Java JSON Data Binding
Java
JSON
POJO
Java JSON Data Binding
Java
JSON
POJO
Java JSON Data Binding
Java
JSON
POJO
Data Binding
Java JSON Data Binding
Java
JSON
POJO Also known as
Mapping
Marshalling / Unmarshalling
JSON Data Binding with Jackson
JSON Data Binding with Jackson
• Spring uses the Jackson Project behind the scenes
JSON Data Binding with Jackson
• Spring uses the Jackson Project behind the scenes
https://github.com/FasterXML/jackson-databind
Jackson Data Binding
Jackson Data Binding
• Package: com.fasterxml.jackson.databind
Jackson Data Binding
• Package: com.fasterxml.jackson.databind
• Maven Dependency
<dependency>
<groupId>com.fasterxml.jackson.core</groupId
>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
Jackson Data Binding
Jackson Data Binding
{
"id": 14,
"firstName": "Mario",
"lastName": "Rossi",
"active": true
}
Jackson Data Binding
{
"id": 14,
Java
"firstName": "Mario", POJO
"lastName": "Rossi",
"active": true
}
Student
Jackson Data Binding
{
"id": 14,
Java
"firstName": "Mario", POJO
"lastName": "Rossi",
"active": true
}
Student
Jackson Data Binding
{
"id": 14,
Java
"firstName": "Mario", POJO
"lastName": "Rossi",
"active": true
}
Student
JSON to Java POJO
JSON to Java POJO
{
"id": 14,
Java
"firstName": "Mario", POJO
"lastName": "Rossi",
"active": true
}
Student
JSON to Java POJO
{ Call
"id": 14, setXXX Java
methods
"firstName": "Mario", POJO
"lastName": "Rossi",
"active": true
}
Student
JSON to Java POJO
{ Call
"id": 14, setXXX Java
methods
"firstName": "Mario", POJO
"lastName": "Rossi",
"active": true
} Jackson will do Student
this work
JSON to Java POJO
JSON to Java POJO
{
"id": 14,
"firstName": "Mario",
"lastName": "Rossi",
"active": true
}
JSON to Java POJO
// getter methods
}
JSON to Java POJO
// getter methods
}
JSON to Java POJO
// getter methods
}
JSON to Java POJO
// getter methods
}
JSON to Java POJO
// getter methods
}
JSON to Java POJO
// getter methods
}
JSON to Java POJO Note: Jackson calls the setXXX methods
It does NOT access internal private fields directly
// getter methods
}
JSON to Java POJO
JSON to Java POJO
import java.io.File;
import com.fasterxml.jackson.databind.ObjectMapper;
JSON to Java POJO
import java.io.File;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
2. Create an instance of
1. Read data from this file this class and populate it
JSON to Java POJO
import java.io.File;
import com.fasterxml.jackson.databind.ObjectMapper;
}
}
JSON to Java POJO
import java.io.File;
import com.fasterxml.jackson.databind.ObjectMapper;
Java
POJO
Student
Java POJO to JSON
• Now, let’s go the other direction
Java
POJO
Student
Java POJO to JSON
• Now, let’s go the other direction
{
"id": 14,
Java
"firstName": "Mario", POJO
"lastName": "Rossi",
"active": true
}
Student
Java POJO to JSON
• Now, let’s go the other direction
{
"id": 14,
Java
"firstName": "Mario", POJO
"lastName": "Rossi", Call
getXXX
"active": true
}
methods Student
Java POJO to JSON
• Now, let’s go the other direction
{
"id": 14,
Java
"firstName": "Mario", POJO
"lastName": "Rossi", Call
getXXX
"active": true
}
methods Student
Jackson will do
this work
Java POJO to JSON
Java POJO to JSON
// create object mapper
ObjectMapper mapper = new ObjectMapper();
File: data/output.json
Jackson calls the getter methods on {
"id": 14,
Student POJO "firstName": "Mario",
"lastName": "Rossi",
to create JSON output file "active": true
}
Spring and Jackson Support
Spring and Jackson Support
m
REST over HTTP
• Most common use of REST is over HTTP
Client Server
My CRM
CRM REST
App Service
HTTP Messages
HTTP Request
Client Message Server
My CRM
CRM REST
App Service
HTTP Messages
HTTP Request
Client Message Server
My CRM
CRM REST
App Service
HTTP Response
Message
HTTP Request Message
Request line
Header variables
Message body
HTTP Request Message
Header variables
Message body
HTTP Request Message
Message body
HTTP Request Message
Message body
• Message body: contents of message
HTTP Response Message
Response line
Header variables
Message body
HTTP Response Message
Response line
Header variables
Message body
HTTP Response Message
Header variables
Message body
HTTP Response Message
Message body
HTTP Response - Status Codes
HTTP Response - Status Codes
Code Range
De
scription
100 - 199 Informational
HTTP Response - Status Codes
Code Range
De
scription
100 - 199 Informational
Code Range
De
scription
100 - 199 Informational
Code Range
De
scription
100 - 199 Informational
Code Range
De
scription
100 - 199 Informational
• Examples
MIME Content Types
• The message format is described by MIME content type
• Examples
• text/html, text/plain
MIME Content Types
• The message format is described by MIME content type
• Examples
• text/html, text/plain
• application/json, application/xml, …
Client Tool
Client Tool
www.getpostman.com
Postman
Free
developer
plan
www.getpostman.com
Install Postman Now
Install Postman Now
www.getpostman.com
Spring REST Controller
m
Spring REST Support
Spring REST Support
• Spring Web MVC provides support for Spring REST
Spring REST Support
• Spring Web MVC provides support for Spring REST
• Extension of @Controller
Spring REST Support
• Spring Web MVC provides support for Spring REST
• Extension of @Controller
• Extension of @Controller
• Extension of @Controller
REST
Client
Spring REST HelloWorld
REST REST
Client Service
Spring REST HelloWorld
/test/hello
REST REST
Client Service
Spring REST HelloWorld
/test/hello
REST REST
Client Service
Hello World!
Spring REST HelloWorld We will write
this code
/test/hello
REST REST
Client Service
Hello World!
Spring REST HelloWorld We will write
this code
/test/hello
REST REST
Client Service
Hello World!
Web Browser
Or
Postman
Spring REST Controller
Spring REST Controller
@RestController
@RequestMapping("/test")
public class DemoRestController {
Spring REST Controller
Adds REST support
@RestController
@RequestMapping("/test")
public class DemoRestController {
Spring REST Controller
Adds REST support
@RestController
@RequestMapping("/test")
public class DemoRestController {
@GetMapping("/hello")
public String sayHello() {
return "Hello World!";
}
}
Spring REST Controller
Adds REST support
@RestController
@RequestMapping("/test")
public class DemoRestController {
Access the REST endpoint at
@GetMapping("/hello")
/test/hello
public String sayHello() {
return "Hello World!";
}
}
Spring REST Controller
Adds REST support
@RestController
@RequestMapping("/test")
public class DemoRestController {
Access the REST endpoint at
@GetMapping("/hello")
/test/hello
public String sayHello() {
return "Hello World!";
}
}
Returns content to
client
Testing with REST Client - Postman
Testing with REST Client - Postman
Testing with REST Client - Postman
Access the REST endpoint at
/test/hello
Testing with REST Client - Postman
Access the REST endpoint at
/test/hello
The response
Testing with REST Client - WebBrowser
Testing with REST Client - WebBrowser
Testing with REST Client - WebBrowser
The response
Web Browser vs Postman
Web Browser vs Postman
• For simple REST testing for GET requests
Web Browser vs Postman
• For simple REST testing for GET requests
m
Development Process
Development Process
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.luv2code.springdemo"
) public class DemoAppConfig {
}
Web App Initializer
Web App Initializer
AbstractAnnotationConfigDispatcherServletInitializer
Web App Initializer (more info)
AbstractAnnotationConfigDispatcherServletInitializer
Web App Initializer (more info)
AbstractAnnotationConfigDispatcherServletInitializer
• Your TO DO list
Web App Initializer (more info)
AbstractAnnotationConfigDispatcherServletInitializer
• Your TO DO list
AbstractAnnotationConfigDispatcherServletInitializer
• Your TO DO list
AbstractAnnotationConfigDispatcherServletInitializer
• Your TO DO list
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { DemoAppConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
Step 3:All Java Config: Servlet Initializer
File:MySpringMvcDispatcherServletInitializer.java
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { DemoAppConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
Step 3:All Java Config: Servlet Initializer
File:MySpringMvcDispatcherServletInitializer.java
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { DemoAppConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
Step 3:All Java Config: Servlet Initializer
File:MySpringMvcDispatcherServletInitializer.java
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { DemoAppConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
Our config class
} from Step 2
}
Step 3:All Java Config: Servlet Initializer
File:MySpringMvcDispatcherServletInitializer.java
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { DemoAppConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
Step 3:All Java Config: Servlet Initializer
File:MySpringMvcDispatcherServletInitializer.java
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { DemoAppConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
Whew!!!!
easy peazy …
Step 4: Create Spring REST Service
Step 4: Create Spring REST Service
@RestController
@RequestMapping("/test")
public class DemoRestController {
@GetMapping("/hello")
public String sayHello() {
return "Hello World!";
}
}
Spring REST Service - Students
m
Create a NewService
Create a NewService
REST
Client
Spring REST Service
REST REST
Client Service
Spring REST Service
/api/students
REST REST
Client Service
Spring REST Service
/api/students
REST REST
Client Service
Spring REST Service We will write
this code
/api/students
REST REST
Client Service
Spring REST Service We will write
this code
/api/students
REST REST
Client Service
Web Browser
Or
Postman
Convert Java POJO to JSON
Convert Java POJO to JSON
Java
POJO
Student
Jackson Data Binding
Jackson Data Binding
{
"id": 14,
"firstName": "Mario",
"lastName": "Rossi",
"active": true
}
Jackson Data Binding
{
"id": 14,
Java
"firstName": "Mario", POJO
"lastName": "Rossi",
"active": true
}
Student
Jackson Data Binding
{
"id": 14,
Java
"firstName": "Mario", POJO
"lastName": "Rossi",
"active": true
}
Student
Jackson Data Binding
{
"id": 14,
Java
"firstName": "Mario", POJO
"lastName": "Rossi",
"active": true
}
Student
Jackson Data Binding
{
"id": 14,
Java
"firstName": "Mario", POJO
"lastName": "Rossi",
"active": true
}
Student
Jackson will do
this work
Spring REST Service
Spring REST Service
REST
Client
Spring REST Service
REST REST
Client Service
Spring REST Service
/api/students
REST REST
Client Service
Spring REST Service We will write
this code
/api/students
REST REST
Client Service
Spring REST Service We will write
this code
/api/students
REST REST
Client Service
…
List<Student>
Spring REST Service We will write
this code
/api/students
REST REST
Client Service
…
List<Student>
Spring REST Service We will write
this code
/api/students
REST REST
Client Service
…
List<Student>
Jackson will
convert to
JSON array
Behind the scenes We will write
this code
REST REST
Client Service
Behind the scenes We will write
this code
REST /api/students
REST
Client Service
Behind the scenes We will write
this code
Spring
REST
REST /api/students
REST
Client Jackson
Service
Behind the scenes We will write
this code
Spring
REST
REST /api/students
REST
Client Jackson
Service
Behind the scenes We will write
this code
Spring
REST
REST /api/students
REST
Client Jackson
Service
…
We will return
List<Student>
Behind the scenes We will write
this code
Spring
REST
REST /api/students
REST
Client Jackson
Service
…
We will return
List<Student>
Behind the scenes We will write
this code
Spring
REST
REST /api/students
REST
Client Jackson
Service
…
We will return
Jackson will convert
List<Student>
List<Student> to
JSON array
Behind the scenes We will write
this code
Spring
REST
REST /api/students
REST
Client Jackson
Service
…
We will return
Jackson will convert
List<Student>
List<Student> to
JSON array
Development Process
Development Process
public Student() {
}
Step 1: Create Java POJO class forStudent
File: Student.java
}
Step 2: Create @RestController
Step 2: Create @RestController
Step 2: Create @RestController
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class StudentRestController {
Step 2: Create @RestController
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class StudentRestController {
@RestController
@RequestMapping("/api")
public class StudentRestController {
@GetMapping("/students")
public List<Student> getStudents() {
Step 2: Create @RestController
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class StudentRestController {
@GetMapping("/students")
public List<Student> getStudents() {
@RestController
@RequestMapping("/api")
public class StudentRestController {
@RestController
@RequestMapping("/api")
public class StudentRestController {
@GetMapping("/students")
public List<Student> getStudents() {
m
Path Variables
Path Variables
Known as a
"path variable"
Path Variables
/api/students/0 Known as a
/api/students/1 "path variable"
/api/students/2
Spring REST Service
Spring REST Service
REST
Client
Spring REST Service
REST REST
Client Service
Spring REST Service
/api/students/{studentId}
REST REST
Client Service
Spring REST Service
/api/students/{studentId}
REST REST
Client Service
{
"firstName": "Mario",
"lastName": "Rossi"
}
Spring REST Service We will write
this code
/api/students/{studentId}
REST REST
Client Service
{
"firstName": "Mario",
"lastName": "Rossi"
}
Spring REST Service We will write
this code
/api/students/{studentId}
REST REST
Client Service
{
"firstName": "Mario",
"lastName": "Rossi"
} Student
Spring REST Service We will write
this code
/api/students/{studentId}
REST REST
Client Service
{
"firstName": "Mario",
"lastName": "Rossi"
} Student
Jackson will
convert to
JSON
Behind the scenes We will write
this code
REST REST
Client Service
Behind the scenes We will write
this code
REST /api/students/{studentId}
REST
Client Service
Behind the scenes We will write
this code
Spring
REST
REST /api/students/{studentId}
REST
Client Jackson
Service
Behind the scenes We will write
this code
Spring
REST
REST /api/students/{studentId}
REST
Client Jackson
Service
Behind the scenes We will write
this code
Spring
REST
REST /api/students/{studentId}
REST
Client Jackson
Service
We will return
Student
Behind the scenes We will write
this code
Spring
REST
REST /api/students/{studentId}
REST
Client Jackson
Service
We will return
Student
Behind the scenes We will write
this code
Spring
REST
REST /api/students/{studentId}
REST
Client Jackson
Service
We will return
Jackson will convert Student
Student to
JSON
Behind the scenes We will write
this code
Spring
REST
REST /api/students/{studentId}
REST
Client Jackson
Service
{
"firstName": "Mario",
"lastName": "Rossi"
}
We will return
Jackson will convert Student
Student to
JSON
Development Process
Development Process
@RestController
@RequestMapping( /api )
public class StudentRestController {
Step 1: Add Request Mapping
File: StudentRestController.java
@RestController
@RequestMapping( /api )
public class StudentRestController {
@RestController
@RequestMapping( /api )
public class StudentRestController {
@GetMapping( /students/{studentId} )
public Student getStudent(@PathVariable int studentId) {
Step 1: Add Request Mapping
File: StudentRestController.java
@RestController
@RequestMapping( /api )
public class StudentRestController {
@GetMapping( /students/{studentId} )
public Student getStudent(@PathVariable int studentId) {
@RestController
@RequestMapping( /api )
public class StudentRestController {
@GetMapping( /students/{studentId} )
public Student getStudent(@PathVariable int studentId) {
@RestController
@RequestMapping( /api )
public class StudentRestController {
@GetMapping( /students/{studentId} )
public Student getStudent(@PathVariable int studentId) {
// populate theStudents
Step 1: Add Request Mapping
File: StudentRestController.java
@RestController
@RequestMapping( /api )
public class StudentRestController {
@RestController
@RequestMapping("/api")
public class StudentRestController {
@GetMapping("/students/{studentId}")
public Student getStudent(@PathVariable int studentId) {
// populate theStudents
return theStudents.get(studentId);
Jackson will convert
} Student to JSON
}
"At the conclusion of this chapter, ensure
completion of all exercises related to REST API
Interview Questions and practice to all Lab
sessions ."