2

这是我的控制器的一部分:

@RequestMapping(value="/json/getPCs/{serverAddress}", method=RequestMethod.GET)
public @ResponseBody List<PC> getPCForServerJSON(@PathVariable String serverAddress) {
    logger.info("Server address: " + serverAddress);
    return PCManager.findByServer(serverManager.findByAddress(serverAddress));
}

在浏览器中我访问 URLhttp://localhost:8080/test/pc/json/getPCs/192.168.200.1

在日志中我看到:

INFO : net.example.test.PCController - Server addres: 192.168.200

如果我转到http://localhost:8080/test/pc/json/getPCs/192.168.200.1/带有斜线的 URL 就可以了:

INFO : net.example.test.PCController - Server addres: 192.168.200.1

为什么?我想使用不带斜线结尾的 url。

4

1 回答 1

1

Configure your RequestMappingHandlerMapping to create PatternsRequestCondition instances (which match your path segments) without matching suffixes.

In a WebMvcConfigurationSupport, override the following

@Override
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
    RequestMappingHandlerMapping mapping = super.requestMappingHandlerMapping();
    mapping.setUseSuffixPatternMatch(false);
    return mapping;
}

That configuration parameter is enabled (true) by default

If enabled a method mapped to "/users" also matches to "/users.*".

So your path segment

{serverAddress}

is actually matching

{serverAddress}[.].*
//              ^ literally a dot

So given the value

192.168.200.1

it captures only

192.168.200

Disabling it will give you the behavior you want.

于 2014-07-25T01:39:14.873 回答