Spring MVC-Request Data

1 带参数的请求

下面指定参数foo,只要含有该参数就行不管是否还有其他参数

    @RequestMapping(value="param", method=RequestMethod.GET)
    public @ResponseBody String withParam(@RequestParam String foo) {
        return "Obtained 'foo' query parameter value '" + foo + "'";
    }

2 带参数组

url=/data/group?param1=foo&param2=bar&param3=baz

@RequestMapping(value="group", method=RequestMethod.GET)
    public @ResponseBody String withParamGroup(JavaBean bean) {
        return "Obtained parameter group " + bean;
    }

JavaBean为带有param1,param2,param3属性的bean

3 带变量

url=../path/+任意变量 与@RequestMapping(value=“/mapping/path/*“相似

    @RequestMapping(value="path/{var}", method=RequestMethod.GET)
    public @ResponseBody String withPathVariable(@PathVariable String var) {
        return "Obtained 'var' path variable value '" + var + "'";
    }

4 带路径变量和参数变量

@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 + "'";
    }

    @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 + "'";
    }

5 @RequestHeader

获取header

@RequestMapping(value="header", method=RequestMethod.GET)
    public @ResponseBody String withHeader(@RequestHeader String Accept) {
        return "Obtained 'Accept' header '" + Accept + "'";
    }

6 @CookieValue

@RequestMapping(value="cookie", method=RequestMethod.GET)
    public @ResponseBody String withCookie(@CookieValue String openid_provider) {
        return "Obtained 'openid_provider' cookie '" + openid_provider + "'";
    }

7 @RequestBody

@RequestMapping(value="body", method=RequestMethod.POST)
    public @ResponseBody String withBody(@RequestBody String body) {
        return "Posted request body '" + body + "'";
    }

$.ajax({ type: "POST", url: form.attr("action"), data: "foo", contentType: "text/plain", dataType: "text", success: function(text) { MvcUtil.showSuccessResponse(text, button); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, button); }});

body=foo

8 request header and body--> HttpEntity

    @RequestMapping(value="entity", method=RequestMethod.POST)
    public @ResponseBody String withEntity(HttpEntity<String> entity) {
        return "Posted request body '" + entity.getBody() + "'; headers = " + entity.getHeaders();
    }
```java
public class HttpEntity<T> {
    public static final HttpEntity EMPTY = new HttpEntity();
    private final HttpHeaders headers;
    private final T body;
       ...

你可能感兴趣的:(spring,Data,MVC-Request)