Java – RequestMapping和变量的展扩顺序

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

Java - Order of expansion of RequestMapping and Variables

问题

Sure, here's the translated code snippet:

我不太确定`@RequestMapping`是如何工作的也无法进行测试但我有这段代码需要理解以便排除问题

如果URL请求为`/view/game1`,`String testString`这一行中,`{service}``"service"`变量是否已经都展开为`/view/game1requestgame1String.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);
}

huangapple
  • 本文由 发表于 2020年10月13日 22:26:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64337154.html
匿名

发表评论

匿名网友

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

确定