Get Method with Params returning null always

Viewed 144

I'm trying to made a search query with a @GetMapping Using JPA and SpringBoot

Here is the controller Method used:

    @GetMapping("/search{min_price}{max_price}{text}")
    public Optional<Product> searchProducts(@PathVariable(required = false) Integer min_price,
                                            @PathVariable(required = false) Integer max_price,
                                            @PathVariable(required = false) String text
    ) {
        Optional<Product> products = productRepository.SearchProduct(min_price, max_price, text);
        return products;
    }

And here the @Query that I made in repository: (I Changed the @Param names to ?X and the issue still happens)

    @Query("SELECT P FROM Product P " +
            "where (P.name like CONCAT('%',lower(?3),'%') or P.description like CONCAT('%',lower(?3),'%')) " +
            "and (?2 is null or P.price <= ?2) " +
            "and (?1 is null or P.price >= ?1)")
    Optional<Product> SearchProduct(@Param("max_price") Integer min_price,
                                    @Param("min_price") Integer max_price,
                                    @Param("text") String text);

I'm using H2 database for the first time, don't know if the ?X is the right way to use the variables in it. (in PL-SQL I use :X for this purpose).

When I made a get the return is always "null" in Postman

this is an example of get that I made "search?min_price=1&max_price=200&text=prod" The database has 5 entries and if I search with this values direct in DB 2 results match the criteria.

1 Answers

Either delimit the path parameters by /

@GetMapping("/search/{min_price}/{max_price}/{text}")

and update your client request to match, or use @RequestParam instead

public Optional<Product> searchProducts(@RequestParam Integer min_price,
                    @RequestParam Integer max_price,
                    @RequestParam String text)
Related