Eduardo Krakhecke
Eduardo Krakhecke

Reputation: 385

Missing URI template variable 'usuarioEntidade' for method parameter of type Long

I try to pass a param in this method here

@RequestMapping(method = RequestMethod.GET, value = "/distrito/{idEntidade}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Collection<Distritos>> buscarTodosDistritos(@PathVariable Long usuarioEntidade) throws ServletException { 

        Collection<Distritos> distritosBuscados = distritosService.buscarFiltro(usuarioEntidade);//parametro, que é o id_entidade, para passar na query de busca distritos
            return new ResponseEntity<>(distritosBuscados, HttpStatus.OK);
    } 

and i got this error

Missing URI template variable 'usuarioEntidade' for method parameter of type Long 

I'm calling this request on my front end right here

 idEntidade = Number(localStorage.getItem("idEntidade"));



$http({
        method : 'GET',
        url : '/user/distrito/' +idEntidade         
    }).then(function(response) {
        $scope.distritos = response.data;

    }, function(response) {
        console.log(response.data);
        console.log(response.status);
    });
}; 

then got an error..

Missing URI template variable 'usuarioEntidade' for method parameter of type Long

Upvotes: 4

Views: 15974

Answers (3)

GDBxNS
GDBxNS

Reputation: 449

Bolded parameters need to have same name

@RequestMapping(method = RequestMethod.GET, value = "/distrito/{idEntidade}", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity> buscarTodosDistritos(@PathVariable Long usuarioEntidade) throws ServletException

correct answer

 @RequestMapping(method = RequestMethod.GET, value = "/distrito/{idEntidade}", produces = MediaType.APPLICATION_JSON_VALUE)
        public ResponseEntity<Collection<Distritos>> buscarTodosDistritos(@PathVariable Long idEntidade) throws ServletException

Upvotes: 0

Rick Ridley
Rick Ridley

Reputation: 583

Your problem is that the name of the path variable in your rest request does not match the name of the variable passed to your java method.

You have two options:

@RequestMapping(method = RequestMethod.GET, value = "/distrito/{idEntidade}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Collection<Distritos>> buscarTodosDistritos(@PathVariable("idEntidade") Long usuarioEntidade)

Or:

@RequestMapping(method = RequestMethod.GET, value = "/distrito/{usuarioEntidade}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Collection<Distritos>> buscarTodosDistritos(@PathVariable Long usuarioEntidade)

Upvotes: 12

Amit K Bist
Amit K Bist

Reputation: 6818

You have to make changed in buscarTodosDistritos() method as below

@PathVariable(value="idEntidade") Long usuarioEntidade  <--- add value in path variable

or

@PathVariable Long idEntidade   <--- or change variable name to map same as the one in the url

Upvotes: 1

Related Questions