java 如何使用 spring 正确管理 PathVariables
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34703568/
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
How to properly manage PathVariables with spring
提问by Bryan
I'm hoping this isn't too simple of a question. I'm new to the java web services world and cant seem to get access to my PathVariables in my controller. I'm using STS and it's not complaining that my syntax is wrong.
我希望这不是一个太简单的问题。我是 Java Web 服务领域的新手,似乎无法访问控制器中的 PathVariables。我正在使用 STS,它并没有抱怨我的语法错误。
So more important than the correct answer, I'd really like to know why this isn't working.
比正确答案更重要的是,我真的很想知道为什么这不起作用。
Here's some example code that I cant get to work:
这是我无法开始工作的一些示例代码:
@RestController
public class TestController {
@RequestMapping("/example")
public String doAThing(
@PathVariable String test
) throws MessagingException {
return "Your variable is " + test;
}
}
If I run a curl against it like so:
如果我像这样对它进行卷曲:
curl http://localhost:8080/example?test=foo
I receive the following response:
我收到以下回复:
{"timestamp":1452414817725,"status":500,"error":"Internal Server Error","exception":"org.springframework.web.bind.MissingPathVariableException","message":"Missing URI template variable 'test' for method parameter of type String","path":"/example"}
{"timestamp":1452414817725,"status":500,"error":"内部服务器错误","exception":"org.springframework.web.bind.MissingPathVariableException","message":"缺少URI模板变量'test ' 对于 String 类型的方法参数","path":"/example"}
I know that I have everything else wired up correctly, other controllers work.
我知道我已经正确连接了其他所有东西,其他控制器可以正常工作。
I feel like I must be missing some fundamental principal here.
我觉得我必须在这里遗漏一些基本原则。
Thanks in advance.
提前致谢。
回答by prem kumar
If you are using path variable, then it has to be part of the URI. As you have not mentioned in the URI but used in the method arguments, spring tries to find out and assign this value from the path URI. But this path variable is not there in the path URI , therefore throwing MissingPathVariableException.
如果您使用路径变量,则它必须是 URI 的一部分。正如您在 URI 中没有提到但在方法参数中使用的那样,spring 尝试从路径 URI 中找出并分配此值。但是这个路径变量不在路径 URI 中,因此抛出 MissingPathVariableException。
This should work.
这应该有效。
@RestController
public class TestController {
@RequestMapping("/example/{test}")
public String doAThing(
@PathVariable String test
) throws MessagingException {
return "Your variable is " + test;
}
}
And your curl request would be like
你的卷曲请求就像
curl http://localhost:8080/example/foo
//here the foo can be replace with other string values
回答by Ralph
Spring support different ways how to map stuff from the url to method parameters: request parameters and path variables
Spring 支持如何将内容从 url 映射到方法参数的不同方式:请求参数和路径变量
Request Parameters are taken from url-query parameters (and request body, for example in http-POST requests). The annotation to mark the java method parameter that should take its value from a request parameter is
@RequestParam
Path Variables (somtimes called path templates) are parts of the url-path. The annotation to mark the java method parameter that should take its value from a request parameter is
@PathVariable
请求参数取自 url-query 参数(和请求正文,例如在 http-POST 请求中)。标记应该从请求参数中获取其值的 java 方法参数的注释是
@RequestParam
路径变量(有时称为路径模板)是 url-path 的一部分。标记应该从请求参数中获取其值的 java 方法参数的注释是
@PathVariable
Have a look at this answerof mine, for an example an links to the Spring Reference.
看看我的这个答案,例如一个指向 Spring 参考的链接。
So what your problem is: you want to read a Request Parameter (from the url-query part), but used the annotation for the Path Variables. So you have to use @RequestParam
instead of @PathVariable
:
那么您的问题是:您想读取请求参数(来自 url-query 部分),但使用了路径变量的注释。所以你必须使用@RequestParam
而不是@PathVariable
:
@RestController
public class TestController {
@RequestMapping("/example")
public String doAThing(@RequestParam("test") String test) throws MessagingException {
return "Your variable is " + test;
}
}
回答by horatius
The reason why it's not working is that there are two ways to pass parameters to a REST API implementation using RestController. One is the PathVariable, the other is RequestParam. Both of them need names to be specified in the RequestMapping annotation.
它不工作的原因是有两种方法可以使用 RestController 将参数传递给 REST API 实现。一个是PathVariable,另一个是RequestParam。它们都需要在 RequestMapping 注释中指定名称。
Check out thisexcellent resource that explains RequestMapping in detail
查看这个详细解释 RequestMapping 的优秀资源
Try this for your solution.
试试这个为您的解决方案。
@RequestMapping("/example/{test}", method= RequestMethod.GET)
public String doAThing(
@PathVariable("test") String test
) throws MessagingException {
return "Your variable is " + test;
}
回答by Ahmed Tawila
The solution for me was:
我的解决方案是:
@RestController
@RequestMapping("/products")
@EnableTransactionManagement
public class ProductController {
@RequestMapping(method = RequestMethod.POST)
public Product save(@RequestBody Product product) {
Product result = productService.save(product);
return result;
}
}