【问题标题】:How to pass multiple path variables for spring boot controller class in GET method?如何在 GET 方法中为 Spring Boot 控制器类传递多个路径变量?
【发布时间】:2018-04-20 07:16:30
【问题描述】:

我试图在 GET 方法中将多个路径变量传递给我的控制器类,所以我通过 POSTMAN 提供变量,当我尝试单个变量时它工作正常,但对于两个变量我得到空结果。

这就是我通过 POSTMAN 传递变量的方式 本地主机:8081/specquestions/java/oops

这里'java'是一个变量,'oops'是另外一个变量

我的 java 控制器类

@RequestMapping(method=RequestMethod.GET,value="/specquestions/{subject}/{topic}")
public ResponseEntity<List<QuestionBank>> getSpecificQuestions(@PathVariable String subject,String topic) {

    return ResponseEntity.ok( questionBankService.getSpecificquestions(subject,topic));

}

谁能告诉我我在哪里做错了。

【问题讨论】:

  • 你忘了用@PathVariable注释第二个参数,就像第一个一样。
  • @JBNizet 非常感谢,是的,现在它正在工作

标签: java spring-boot postman


【解决方案1】:

只需为控制器中的第二个参数添加@PathVariable,如下所示

@RequestMapping(method=RequestMethod.GET,value="/specquestions/{subject}/{topic}")
    public ResponseEntity<List<QuestionBank>> getSpecificQuestions(@PathVariable String subject,@PathVariable String topic) {

        return ResponseEntity.ok( questionBankService.getSpecificquestions(subject,topic));

    }

【讨论】:

    猜你喜欢
    • 2013-09-05
    • 1970-01-01
    • 2022-10-17
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    相关资源
    最近更新 更多