Sercan Ozdemir
Sercan Ozdemir

Reputation: 4692

Spring MVC Abstract Controller PathVariable

I've an abstract controller and have path variables in some of it's operations:

@Controller
@RequestMapping("/generic-status/v1")
public abstract class GenericStatusController{

    @RequestMapping(value = "/connection/availability/{connectionName}", method = { RequestMethod.GET })
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public abstract ConnectionResult checkConnection( @PathVariable("connectionName") String connectionName ) throws ConnectionException, ParameterInvalidException, StatusApiException;

When I extend and use it all request mappings are working properly. But unfortunately path variable always come as null.

Could you please help me in this ?

Thanks in advance

Upvotes: 2

Views: 1109

Answers (1)

Daniel Hajduk
Daniel Hajduk

Reputation: 336

As M.Deinum pointed out: From what ive deducted after extending this abstract controller you have something like this:

@Override
ConnectionResult checkConnection(String connectionName) throws ConnectionException, ParameterInvalidException, StatusApiException; {
     // logic
}

Which means that the instance of controller you will be using has actually no @PathVariable mapping describing and telling Spring it should provide it as an argument to the method. Solution: Add @PathVariable also in extending controller:

@Override
ConnectionResult checkConnection(@PathVariable("connectionName") String connectionName) throws ConnectionException, ParameterInvalidException, StatusApiException; {
     // logic
}

Upvotes: 2

Related Questions