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

Rafik BELDI picture Rafik BELDI · Nov 11, 2015 · Viewed 28.4k times · Source

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.

Answer

Viresh picture Viresh · Nov 11, 2015

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) {
  ...
}