YDP API&MS UNIT v[1] [Read-Only] [Compatibility Mode]
YDP API&MS UNIT v[1] [Read-Only] [Compatibility Mode]
UNIT IV
Spring REST
Y DURGA PRASAD
Associate Professor
Department of Computer Science and Engineering
ADITYA
RESTful Web Services are client and server applications that communicate over the WWW. RESTful Web Services
are REST Architecture based Web Services. In REST Architecture, everything is a resource. RESTful Web Services
provides communication between software applications running on different platforms and frameworks. We can
consider web services as code on demand. A RESTful Web Service is a function or method which can be called by
sending an HTTP request to a URL, and the service returns the result as the response.
REST stands for REpresentational State Transfer. It is developed by Roy Thomas Fielding, who also developed
HTTP. The main goal of RESTful web services is to make web services more effective. RESTful web services try to
define services using the different concepts that are already present in HTTP. REST is an architectural approach,
not a protocol.
We can build REST services with both XML and JSON. JSON is more popular format with REST. The key
abstraction is a resource in REST. A resource can be anything. It can be accessed through a Uniform Resource
Identifier (URI).
2
ADITYA
Advantages of RESTful web services
❑ RESTful web services are platform-independent.
❑ It can be written in any programming language and can be executed on any platform.
❑ It provides different data format like JSON, text, HTML, and XML.
❑ It is fast in comparison to SOAP because there is no strict specification like SOAP.
❑ These are reusable.
❑ They are language neutral.
Any class that needs to be exposed as a RESTful resource has to be annotated with @RestController
@RestController
❑This annotation is used to create REST controllers.
❑It is applied on a class in order to mark it as a request handler/REST resource.
❑This annotation is a combination of @Controller and @ResponseBody annotations.
❑@ResponseBody is responsible for the automatic conversion of the response to a JSON string literal. If @Restcontroller is
in place, there is no need to use @ResponseBody annotation to denote that the Service method simply returns data, not a
view.
❑@RestController is an annotation that takes care of instantiating the bean and marking the same as REST controller.
❑It belongs to the package, org.springframework.web.bind.annotation.RestController
3
ADITYA
While sending a response, we may like to set the HTTP status code and headers .To help achieving this, we
can use ResponseEntity class.
ResponseEntity<T> Will help us add a HttpStatus status code and headers to our response.
4
ADITYA
Methods of ResponseEntity
5
ADITYA
❑@RequestBody annotation is used to indicating a method parameter should be bind to the body of the HTTP
request. Internally, this annotation uses HTTP Message converters to convert the body of HTTP requests to
domain objects.
{ "firstName" : "Elmer", "lastName" : "Fudd" }
❑Assume that we are sending this JSON in the request body, now inside the controller, we can bind this JSON
data to a domain object.
❑@PostMapping("/users") public void printData(@RequestBody User user) { }
❑Now this will happen with the help of Jackson API which is present in the classpath. Spring would convert the
incoming JSON to a User object from the request body (because we added the @RequestBody annotation)
❑RequestBody is of course not limited to JSON, It can handle multiple formats, including plain text and XML, but
JSON is probably the most used format.
@PostMapping(consumes="application/json")
public ResponseEntity<String> createCustomer( @RequestBody CustomerDTO customerDTO)
{
// logic goes here
} 6
ADITYA
consumes attribute can be supplied with a single value or an array of media types as shown below
consumes = "text/plain"
or
consumes = {"text/plain", "application/json"}
Some more valid values:
consumes = "application/json"
consumes = {"application/xml", "application/json"}
consumes = {"text/plain", "application/*"}
produces is used to specify the MIME media types or representations a resource can produce and send back to
the client
alid values for produces attribute:
produces = "text/plain"
produces = {"text/plain", "application/json"}
produces = {"application/xml", "application/json"}
7
ADITYA