重要结论:
SpringMVC路径参数除了可以通过@PathVariable获取之外,还可以通过对象属性获取。即注意不要让路径中的属性与对象属性冲突了哟!!!
验证过程:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.io.Serializable;
/**
* 关于路径参数获取{version}有趣的遇见
*/
@RestController
@RequestMapping(value = "/v{version}/config")
public class TestController {
/**
* 日志记录器
*/
private static final Logger logger = LoggerFactory.getLogger(TestController.class);
/**
* 使用@PathVariable获取
*
* @param version
* @return
*/
@RequestMapping(value = "find01", method = RequestMethod.GET)
public String find01(@PathVariable String version) {
logger.info("version:{}", version);
return "SUCCESS";
}
/**
* 使用属性获取
*
* @param version
* @return
*/
@RequestMapping(value = "find02", method = RequestMethod.GET)
public String find02(String version) {
logger.info("version:{}", version);
return "SUCCESS";
}
/**
* 使用@PathVariable与对象属于获取
*
* @param configDto
* @return
*/
@RequestMapping(value = "find03", method = RequestMethod.GET)
public String find03(@PathVariable String version, ConfigDto configDto) {
logger.info("version:{}---configDto.getVersion:{}", version, configDto.getVersion());
return "SUCCESS";
}
/**
* 使用对象属于获取
*
* @param configDto
* @return
*/
@RequestMapping(value = "find04", method = RequestMethod.GET)
public String find04(ConfigDto configDto) {
logger.info("version:{}", configDto.getVersion());
return "SUCCESS";
}
/**
* 使用对象属于获取
*
* @param configDto
* @return
*/
@RequestMapping(value = "find05/{name}", method = RequestMethod.GET)
public String find05(ConfigDto configDto) {
logger.info("name:{}---version:{}", configDto.getName(), configDto.getVersion());
return "SUCCESS";
}
/**
* 使用对象属于获取
*
* @param configDto
* @return
*/
@RequestMapping(value = "do06", method = RequestMethod.POST)
public String do06(ConfigDto configDto) {
logger.info("version:{}", configDto.getVersion());
return "SUCCESS";
}
}
class ConfigDto implements Serializable {
/**
* 版本名称
*/
private String name;
/**
* 配置版本
*/
private String version;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}
一、使用@PathVariable获取
请求:http://localhost:8080/test/v113213213/config/find01
日志结果:version:113213213
二、使用属性获取
请求:http://localhost:8080/test/v113213213/config/find02
日志结果:version:null
三、使用@PathVariable与对象属于获取
请求:http://localhost:8080/test/v113213213/config/find03
日志结果:version:113213213---configDto.getVersion:113213213
四、使用对象属于获取
请求:http://localhost:8080/test/v113213213/config/find04
日志结果:version:113213213
五、使用@PathVariable与对象属于获取
请求:http://localhost:8080/test/v113213213/config/find05/test
日志结果:name:test---version:113213213
六、使用对象属于获取 POST方法
post请求:http://localhost:8080/test/v113213213/config/do6
日志结果:version:113213213
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。