本章介绍后台如何接受前台传入的参数(普通传参,RESTFUL路径传参数)
url参数
@RequestMapping(value="param", method=RequestMethod.GET)
public @ResponseBody String withParam(@RequestParam String foo) {
return "Obtained 'foo' query parameter value '" + foo + "'";
}
通过@RequestParam
可以获得url中传递的参数并自动转型,@RequestParam
对应的方法参数名必须与url参数名完全一样,否则将会报错。如果有多个参数,就在方法中加@RequestParam
即可。
将参数自动封装成javabean
@RequestMapping(value="group", method=RequestMethod.GET)
public @ResponseBody String withParamGroup(JavaBean bean) {
return "Obtained parameter group " + bean;
}
如果方法的参数是一个JavaBean Spring Mvc会将前台传来的参数自动转型,并设置到JavaBean对应的属性上。JavaBean对应的属性名必须完全和参数名一样。
路径变量
SpringMVC 对RESTFUL的支持非常强大,通过路径变更可以非常方便获得RESTFUL url中传递的参数。
@RequestMapping(value="path/{var}", method=RequestMethod.GET)
public @ResponseBody String withPathVariable(@PathVariable String var) {
return "Obtained 'var' path variable value '" + var + "'";
}
在@RequestMapping
中指定的路径将参数用{var}
代替,在方法参数中使用@PathVariable
获得路径中的变量,方法参数名必须和{var}
中的变量一样。
Matrix 参数
Matrix 参数是指在url中以;分割的参数,例:
http://moremaps.com/us/ma/cambridge;scale=50000;roads=main
SpringMVC支持获取Matrix参数 使用@MatrixVariable
获取Matrix参数
@RequestMapping(value="{path}/simple", method=RequestMethod.GET)
public @ResponseBody String withMatrixVariable(@PathVariable String path, @MatrixVariable String foo) {
return "Obtained matrix variable 'foo=" + foo + "' from path segment '" + path + "'";
}
通过如上代码可以获取foo参数
http://localhost:8080/data/matrixvars;foo=bar/simple;
@RequestMapping(value="{path1}/{path2}", method=RequestMethod.GET)
public @ResponseBody String withMatrixVariablesMultiple (
@PathVariable String path1, @MatrixVariable(value="foo", pathVar="path1") String foo1,
@PathVariable String path2, @MatrixVariable(value="foo", pathVar="path2") String foo2) {
return "Obtained matrix variable foo=" + foo1 + " from path segment '" + path1
+ "' and variable 'foo=" + foo2 + " from path segment '" + path2 + "'";
}
通过如上代码可以获取
http://localhost:8080/data/matrixvars;foo=bar1/multiple;foo=bar2; 中的多个参数
获取 header中的参数
通过@RequestHeader
可以获取 header中的参数,@RequestHeader
对应的参数名必须和header中的参数名一样。
@RequestMapping(value="header", method=RequestMethod.GET)
public @ResponseBody String withHeader(@RequestHeader String Accept) {
return "Obtained 'Accept' header '" + Accept + "'";
}
获取 cookie
通过@CookieValue
可以得到cookie值
@RequestMapping(value="cookie", method=RequestMethod.GET)
public @ResponseBody String withCookie(@CookieValue String openid_provider) {
return "Obtained 'openid_provider' cookie '" + openid_provider + "'";
}
获取 json数据
通过@RequestBody
可以获取前台传入的json数据
获取Http headers 和Body
如果方法参数是HttpEntity<String>则可以获取Http实体,通过实体可以得到请求的headers 和body
@RequestMapping(value="entity", method=RequestMethod.POST)
public @ResponseBody String withEntity(HttpEntity<String> entity) {
return "Posted request body '" + entity.getBody() + "'; headers = " + entity.getHeaders();
}
自定义参数解析
SpringMVC允许自定义参数解析,参数解析类需要实现HandlerMethodArgumentResolver
接口,实现supportsParameter
来确定满足什么条件调用该解析类,实现resolveArgument
来完成参数解析操作。
要实现参数自定义解析首先需要注册参数解析器,argument-resolvers
来注册解析器
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven conversion-service="conversionService" enable-matrix-variables="true">
<argument-resolvers>
<beans:bean class="org.springframework.samples.mvc.data.custom.CustomArgumentResolver"/>
</argument-resolvers>
</annotation-driven>
实现参数解析器,只要方法参数有@RequestAttribute
注解即调用参数转换器解析参数
public class CustomArgumentResolver implements HandlerMethodArgumentResolver {
public boolean supportsParameter(MethodParameter parameter) {
return parameter.getParameterAnnotation(RequestAttribute.class) != null;
}
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception {
RequestAttribute attr = parameter.getParameterAnnotation(RequestAttribute.class);
return webRequest.getAttribute(attr.value(), WebRequest.SCOPE_REQUEST);
}
}
使用方法如下
@ModelAttribute
void beforeInvokingHandlerMethod(HttpServletRequest request) {
request.setAttribute("foo", "bar");
}
@RequestMapping(value="/data/custom", method=RequestMethod.GET)
public @ResponseBody String custom(@RequestAttribute("foo") String foo) {
return "Got 'foo' request attribute value '" + foo + "'";
}