Java Spring如何使用@GetMapping获取带名称的映射

huangapple go评论110阅读模式
英文:

Java Spring how to getmapping with name

问题

如何使得 @getmapping 不带有 id,而是通过(String)名称进行匹配......

@GetMapping("/employees/{id}")

Employee one(@PathVariable Long id) {
return repository.findById(id)
.orElseThrow(() -> new EmployeeNotFoundException(id));
}

英文:

how can I make @getmapping without id and by (String) name .....

@GetMapping("/employees/{id}")

Employee one(@PathVariable Long id) {
    return repository.findById(id)
            .orElseThrow(() -> new EmployeeNotFoundException(id));
}

答案1

得分: 0

你可以以与目前相同的方式进行只需将 id 替换为 name

    @GetMapping("/employees/{name}")
    
    Employee one(@PathVariable String name) {
        ...
    }
英文:

You can do it the same way you already are but switch out id with name

@GetMapping("/employees/{name}")

Employee one(@PathVariable String name) {
    ...
}

huangapple
  • 本文由 发表于 2020年9月3日 01:33:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63710789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定