Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between @Param and @RequestParam in Spring

Tags:

spring

What is the use of @Param.
As i saw a question regarding this.
and i am not able to find the relative answer..
http://localhost:8080/MyWebApp/main.obj?name=priya

We can use @RequestParam to retrieve the value.
My question is that can @Param be used?

like image 868
Priya Rastogi Avatar asked Jun 12 '26 00:06

Priya Rastogi


2 Answers

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.

In your case, you are trying to fetch URL Parameter value. @RequestParam need to be used. @RequestParam is used to bind method parameter to web URL request parameter.

like image 97
Vijayaraaghavan Manoharan Avatar answered Jun 13 '26 12:06

Vijayaraaghavan Manoharan


In Spring Data Rest @Param is used to extract values from request parameters in a RESTful endpoint method.

    @RepositoryRestResource
    interface ProductRepository extends JpaRepository<Product, Long> {
       @RestResource(path="/findByName")
       Page<Product> findByNameContains(@Param("productName") String 
       name, 
       Pageable pageable);
   }

you can now access the endpoint /findByName?productName=name to search for YourEntity resources

like image 38
Pandurov Avatar answered Jun 13 '26 13:06

Pandurov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!