英文:
Java - Order of expansion of RequestMapping and Variables
问题
Sure, here's the translated code snippet:
我不太确定`@RequestMapping`是如何工作的,也无法进行测试,但我有这段代码,需要理解以便排除问题。
如果URL请求为`/view/game1`,在`String testString`这一行中,`{service}`和`"service"`变量是否已经都展开为`/view/game1,request,game1,String.class`?就是在`@RequestMapping`期间展开,然后在`testString`初始化期间再次展开?
还有在哪个阶段发生了展开?有`@RequestMapping`,然后是`testString`,最后是`MyClass service = MyClass.getName(testString)`。
@RequestMapping("/view/{service}")
public ServiceResponse TESTService(HttpServletRequest request, HttpServletResponse response)
{
String testString = bindTestVariable("/view/{service}", request, "service", String.class);
MyClass service = MyClass.getName(testString);
return service;
}
Please note that the translation provided is purely based on the code snippet you provided and does not include the explanation you gave in your original message. If you have any further requests or questions, feel free to ask!
英文:
I'm not sure how @RequestMapping
works and can't test this but I have the code and need to understand to troubleshoot something.
If the URL request is /view/game1
and in the line String testString
is the {service}
and "service"
variable already both expanded to /view/game1, request, game1, String.class
? Like is it expanded during @RequestMapping
and then also expanded during testString
initialization?
Also during which stage is the expansion happening? There is @RequestMapping
then testString
and MyClass service = MyClass.getName(testString)
.
@RequestMapping("/view/{service}")
public ServiceResponse TESTService(HttpServletRequest request, HttpServletResponse response)
{
String testString = bindTestVariable("/view/{service}", request, "service", String.class);
MyClass service = MyClass.getName(testString);
return service;
}
答案1
得分: 1
在那段代码中,并没有进行"扩展"。代码中进行了"提取"(或"解析")URL路径的一部分操作,这发生在调用bindTestVariable()
方法时。
然而,你应该让 Spring 来处理,可以使用 @PathVariable
。
另外,Java 的命名约定是方法名以小写字母开头。
@GetMapping("/view/{service}")
public ServiceResponse testService(@PathVariable("service") String service)
{
return MyClass.getName(service);
}
英文:
There is no "expansion" going on in that code. There is "extraction" (or "parsing") of part of the URL path, which happens when the bindTestVariable()
method is called.
You should however let Spring do that, using @PathVariable
.
Also, Java naming convention is for method names to start with lowercase letter.
@GetMapping("/view/{service}")
public ServiceResponse testService(@PathVariable("service") String service)
{
return MyClass.getName(service);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论