Posts

Showing posts with the label Spring Web Annotations

Spring @RequestHeader Annotation Example

Image
In this section we will learn about  @RequestHeader  Annotation.   Spring MVC provides an annotation  @RequestHeader  for facilitating use to get the header details easily in our controller class. This annotation would bind the header details with the method arguments, and it can be used inside the methods. Given below are the available fields that you can pass optionally defaultValue : The default value to use as a fallback. name : The name of the request header to bind to. required : Whether the header is required. value : Alias for name If the method parameter is  Map<String, String> , or  HttpHeaders  then the map is populated with all header names and values. @RestController public class UserController { @GetMapping ( "/users" ) public User getUser ( @RequestHeader String token ) { //TODO } } The following example creates a Spring Boot web application which uses @RequestHeader . The application receives an URL from which it builds a json res

Spring @RestController Annotation Example

Image
In this section we will learn about  @RestController  Annotation.   The  @RestController   annotation is mainly utilized for building restful web services utilizing Spring MVC. It is a convenience annotation, this annotation itself annotated with @ResponseBody and @Controller annotation. The class annotated with @RestController annotation returns JSON replication in all the methods. @RestController public class UserController { @GetMapping ( "/users" ) public User getUser ( ) { //TODO } } The following example creates a Spring Boot web application which uses @RestController .  Project Directory Pom.xml <? xml version ="1.0" encoding ="UTF-8" ?> < project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns: xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi :schemaLocation ="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion &g

Spring @RequestParam Annotation Example

Image
In this section we will learn about @RequestParam  Annotation.   The  @RequestParam   annotation enables spring to extract input data that may be passed as a query, form data, or any arbitrary custom data. http: //localhost:8080/api/users?name=Sibin&email=sibin@gmail.in If we break the above URL down into smaller parts, then we will see that: HTTP  – is the protocol being used, localhost   – is the domain name, 8080 –  is the port number, api  – is the root path of your web services application, users –  is most likely the @Path value of your Root Resource class and, name  – is the URL Query parameter which we can read with @RequestParam annotation, email – is the URL Query parameter which we can also read with @RequestParam annotation. The following example creates a Spring Boot web application which uses  @RequestParam annotation . Project Directory Pom.xml <? xml version ="1.0" encoding ="UTF-8" ?> < project xmlns ="http://maven.apache.org/POM/4

Spring @PathVariable Annotation Example

Image
In this section we will learn about  @ PathVariable  Annotation.   The  @ PathVariable   annotation used on a method argument to bind it to the value of a URI template variable. It has the following optional elements: name  - name of the path variable to bind to required  - tells whether the path variable is required value  - alias for name With the  @PathVariable  annotation, we bind the request URL template path variable to the method variable.  For instance, with the  /India/Knowledgefactory/  URL, the  India  value is bind to the  country  variable and " Knowledgefactory " value to the  name  variable. @GetMapping ( path = " /users/{country}/{name} " ) public User getUserByCountryAndName( @PathVariable String country, @PathVariable ( name = " name " ) String name) { ........... } The following example creates a Spring Boot web application which uses @PathVariable . The application receives an URL from which it buil