【发布时间】:2016-04-14 16:50:05
【问题描述】:
我希望这不是一个太简单的问题。我是 Java Web 服务世界的新手,似乎无法访问我的控制器中的 PathVariables。我正在使用 STS,它并没有抱怨我的语法错误。
比正确答案更重要,我真的很想知道为什么这不起作用。
这是一些我无法工作的示例代码:
@RestController
public class TestController {
@RequestMapping("/example")
public String doAThing(
@PathVariable String test
) throws MessagingException {
return "Your variable is " + test;
}
}
如果我像这样对它进行卷曲:
curl http://localhost:8080/example?test=foo
我收到以下回复:
{"timestamp":1452414817725,"status":500,"error":"内部服务器 错误","异常":"org.springframework.web.bind.MissingPathVariableException","消息":"丢失 用于方法参数类型的 URI 模板变量“测试” 字符串","路径":"/example"}
我知道我已经正确连接了所有其他东西,其他控制器也可以正常工作。
我觉得我必须在这里遗漏一些基本原则。
提前致谢。
【问题讨论】:
-
是:您在请求中使用的是查询参数 (
@RequestParam) 而不是路径变量。 -
对我来说,当我错误地使用
@PathVariable而不是@RequestParam时发生 MissingPathVariableException