Function Annotations - AyushiDamele/Spring GitHub Wiki

@RequestParam annotation used for accessing the query parameter values from the request. Look at the following request URL: The following are the list of parameters supported by the @RequestParam annotation:

  • defaultValue – This is the default value as a fallback mechanism if request is not having the value or it is empty.
  • name – Name of the parameter to bind
  • required – Whether the parameter is mandatory or not. If it is true, failing to send that parameter will fail.
  • value – This is an alias for the name attribute.

@PathVariable identifies the pattern that is used in the URI for the incoming request. Let’s look at the below request URL: http://localhost:8080/springmvc/hello/101?param1=10&param2=20

    @RequestMapping("/hello/{id}") 
      public String getDetails(@PathVariable(value="id") String id,
      @RequestParam(value="param1", required=true) String param1, 
      @RequestParam(value="param2", required=false) String param2) {
     .......
    }

@Param is used to bind the method parameter to Query parameter.

In Spring Framework, @Param (org.springframework.data.repository.query.Param) is used to bind the method parameter to Query parameter.

Example:

@Query("select e from Employee e where e.deptId = :deptId")
List<Employee> findEmployeeByDeptId(@Param("deptId") Long departmentId);

Here, Employee is JPA Entity, and @Param is used to bind the method parameter departmentId to Query parameter deptId.

@RequestBody annotated parameters get linked to the HTTP request body. Parameter values are converted to the declared method argument type using HttpMessageConverters. This annotation indicates a method parameter should be bound to the body of the web request. For example Angular request for Spring RequestBody would look like that:

$scope.user = {
            username: "foo",
            auth: true,
            password: "bar"
        };    
$http.post('http://localhost:7777/scan/l/register', $scope.user).
                        success(function (data, status, headers, config) {
                            ...
                        })

Endpoint with RequestBody:

@RequestMapping(method = RequestMethod.POST, produces = "application/json", 
                value = "/register")
public Map<String, String> register(Model uiModel,
                                    @RequestBody User user,
                                    HttpServletRequest httpServletRequest) {... 

⚠️ **GitHub.com Fallback** ⚠️