java Long 类型的方法参数缺少 URI 模板变量“usuarioEntidade”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46737701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:20:32  来源:igfitidea点击:

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

javaspringspring-mvcspring-boot

提问by Eduardo Krakhecke

I try to pass a param in this method here

我尝试在此方法中传递一个参数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

回答by Rick Ridley

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.

您的问题是您的休息请求中路径变量的名称与传递给您的 java 方法的变量名称不匹配。

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)

回答by Amit K Bist

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

您必须在 buscarTodosDistritos() 方法中进行如下更改

@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

回答by GDBxNS

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

@RequestMapping(method = RequestMethod.GET, value = "/distrito/{ idEntidade}", products= MediaType.APPLICATION_JSON_VALUE) public ResponseEntity> buscarTodosDistritos(@PathVariable Long usuarioEntidade) 抛出 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