J
J
Jake Taylor2021-10-15 22:07:51
Java
Jake Taylor, 2021-10-15 22:07:51

How to check GET parameter name of a request in Sping?

For mapping a GET request, I use the annotation @GetMapping:

@GetMapping(params = {ParameterName.SORT_BY})
    public List<GiftCertificate> findAllSorted(@RequestParam(value = ParameterName.SORT_BY) Set<ColumnName> columnNames) {

return service.findAllSorted(columnNames);
    }


ParameterName.SORT_BYis a regular enum.

On request:

http://localhost:8080/gift-certificates?sort_by=name


I get into this method, everything is fine. But why and for a request that has a parameter that is not in the mapping:

http://localhost:8080/gift-certificates?sort_ABRAKADABRA_by=name


the request gets into this method:
@GetMapping
    public List<GiftCertificate> findAll() {
        return service.findAll();
    }

Why and how to check GET request parameters?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BorLaze, 2021-10-15
@BorLaze

Here is the cat, I gave you the direction to read in the previous question !
Once again - what do the declarations in your code do:
gift-certificates, as I understand it, this is a global prefix for the controller
@GetMapping(params = {ParameterName.SORT_BY})means: call this method when the url looks like gift-certificates?sort_by
When the url does not match, the general one is taken - @GetMapping- which is responsible for gift-certificates, since all parameters after the question mark are optional .
If you need to somehow catch parameters with arbitrary names - then study this article - typical cases are just analyzed there (in particular, point 6).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question