Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Rest Controller, Path Variables on an overriden method's arguement

I have a controller annotated with @RestController and it implements an interface:

 public interface ContratEndpoint {

      String ROOT = "/api/contrats";

      String GET_CONTRAT = "";

      String GET_CONTRAT_PER_PK = "/{idContrat}";

      @RequestMapping(value = GET_CONTRAT)
      Contrat getContrat(@RequestParam(value = "contratId")Long contratId);


      @RequestMapping(value = GET_CONTRAT_PER_ID)
      ExtContrat getContratById(@PathVariable("idContrat") Long idContrat);
}

The controller:

@RestController
@RequestMapping(value = ContratEndpoint.ROOT)
public class ContratController implements ContratEndpoint {

    //Injecting Services....

    @Resource
    private Mapper mapper;


    @Override
    public Contrat getContrat(Long contratId) {
    return mapper.map(contratService.get(contratId),Contrat.class);
    }

    @Override
    public ExtContrat getContratById(@PathVariable("idContrat") Long idContrat){
        Preconditions.checkArgument(idContrat !=null);
        return mapper.map(contratService.get(idContrat),ExtContrat.class);
    }

.The above Code works just fine.

. But For the first inherited method , I didn't have to annotate arguments with @RequestParam and it worked just fine.

As for the second method I tried at first :

 @Override
        public ExtContrat getContratById(Long idContrat){
            Preconditions.checkArgument(idContrat !=null);
            return mapper.map(contratService.get(idContrat),ExtContrat.class);
        }

. I expected the same behaviour Like the first Method, But i was wrong and the code ended up firing an IllegalArgumentException because of the check in ligne Preconditions.checkArgument(idContrat!=null).

My question is what is so specific about @PathVariable that i've missed ? Or is it just something is wrong with my approach?

Thanks.

like image 504
Rafik BELDI Avatar asked Nov 11 '15 13:11

Rafik BELDI


People also ask

Which are the valid arguments to a Spring controller method?

The following are the supported method arguments: Request or response objects (Servlet API). Choose any specific request or response type, for example ServletRequest or HttpServletRequest . Session object (Servlet API): of type HttpSession .

What is difference between @PathParam and @PathVariable?

The @PathVariable annotation is used for data passed in the URI (e.g. RESTful web services) while @RequestParam is used to extract the data found in query parameters. These annotations can be mixed together inside the same controller. @PathParam is a JAX-RS annotation that is equivalent to @PathVariable in Spring.

What is @RequestMapping annotation used for?

RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods.

What is @PathVariable annotation in Spring boot?

The @PathVariable annotation is used to extract the value of the template variables and assign their value to a method variable. A Spring controller method to process above example is shown below; @RequestMapping("/users/{userid}", method=RequestMethod.


1 Answers

There is difference between Request param and path variable,seee below post that you can confirm with your uri the cause for the exception :

@PathVariable is to obtain some placeholder from the uri (Spring call it an URI Template) — see Spring Reference Chapter 16.3.2.2 URI Template Patterns
@RequestParam is to obtain an parameter — see Spring Reference Chapter 16.3.3.3 Binding request parameters to method parameters with @RequestParam

Assume this Url http://localhost:8080/SomeApp/user/1234/invoices?date=12-05-2013 (to get the invoices for user 1234 for today)

@RequestMapping(value="/user/{userId}/invoices", method = RequestMethod.GET)
public List<Invoice> listUsersInvoices(
            @PathVariable("userId") int user,
            @RequestParam(value = "date", required = false) Date dateOrNull) {
  ...
}
like image 141
Viresh Avatar answered Oct 06 '22 00:10

Viresh