Spring 处理请求和响应相关的注解

@Controller

默认返回 templates 目录下的 string.html 页面内容。
在方法中加上 @ResponseBody 注解,可以返回JSON、XML或自定义mediaType的内容

@RestController

直接返回内容,会自动将对象实体转换为JSON格式,视图解析器 InternalResourceViewResolver 不起作用。
@RestController = @Controller + @ResponseBody

@RequestBody

接收请求体中的 JSON 数据,通过实体类的setter方法赋值给属性。
json 的 "" => 实体 String 为 ""
json 的 "" => 实体 Integer、Double 为 null
json 的 null => 实体为 null
@RequestBody 可以与 @RequestParam() 同时使用,@RequestBody 最多只能有一个,@RequestParam() 可以有多个。

以 String 接收数据

@RequestMapping("/index")
public String indexMapping(@RequestBody String jsonStr) {
    return jsonStr;
}

以对象实体接收数据

// {"name":"hanmeimei","age":12}
@RequestMapping("/index")
public String indexMapping(@RequestBody User user) {
    return user.toString();
}

以复杂的对象实体接收数据

public class Team {
    private Integer id;
    private String name;
    private List<String> honors;
    private List<User> members;
}

// {
//     "id": 1,
//     "name": "good",
//     "honors": ["very good", "very fast"],
//     "members": [{"name":"hanmeimei","age":12},
//                 {"name":"lilei","age":13}],
// }
@RequestMapping("/index")
public String indexMapping(@RequestBody Team team) {
    return team.toString();
}

@ResponseBody

将对象实体转换为JSON、XML或自定义mediaType的内容,并在 HTTP response body 中返回

@RequestMapping

将请求映射到控制器上,可以在控制器类和/或方法上使用。

处理单个请求

@RequestMapping("/home")
public class IndexController {
    @RequestMapping("/index")
    String indexMapping() {
        return "Hello from index mapping.";
    }
}

处理多个请求

@RequestMapping("/home")
public class IndexController {
    @RequestMapping(value = {
        "/",
        "/index",
        "/index/*.html",
        "/index/**/*.html"
    })
    String indexMultipleMapping() {
        return "Hello from index multiple mapping.";
    }
}

处理请求类型

默认是 HTTP GET 类型的。

@RequestMapping(value = "/home", method = RequestMethod.GET)
String get() {}

@RequestMapping(value = "/home", method = RequestMethod.DELETE)
String delete() {}

@RequestMapping(value = "/home", method = RequestMethod.POST)
String post() {}

@RequestMapping(value = "/home", method = RequestMethod.PUT)
String put() {}

@RequestMapping(value = "/home", method = RequestMethod.PATCH)
String patch() {}

处理请求类型快捷方式

@GetMapping(value = "/home")
String get() {}

@DeleteMapping(value = "/home")
String delete() {}

@PostMapping(value = "/home")
String post() {}

@PutMapping(value = "/home")
String put() {}

@PatchMapping(value = "/home")
String patch() {}

处理生产和消费对象

public class IndexController {
    // 生产 application/JSON 响应
    @RequestMapping(value = "/prod", produces = {
        "application/JSON"
    })
    @ResponseBody
    String getProduces() {
        return "Produces attribute";
    }

    // 消费 application/JSON & application/XML 请求
    @RequestMapping(value = "/cons", consumes = {
        "application/JSON",
        "application/XML"
    })
    @ResponseBody
    String getConsumes() {
        return "Consumes attribute";
    }
}

处理消息头

public class IndexController {
    // 处理 Content-Type=application/json 的请求
    @RequestMapping(value = "/head", headers = {
        "Content-Type=application/json"
    })
    String head() {
        return "Mapping applied along with headers";
    }
}
public class IndexController {
    @RequestMapping(value = "/head", headers = {
        "Content-Type=text/plain",
        "Content-Type=application/json"
    })
    String head() {
        return "Mapping applied along with headers";
    }
}

处理请求参数

public class IndexController {
    @RequestMapping(value = "/fetch", params = {
        "personId=10"
    })
    String getParams10(@RequestParam("personId") String id) {
        return "Fetched parameter using params attribute = " + id;
    }

    @RequestMapping(value = "/fetch", params = {
        "personId=20"
    })
    String getParams20(@RequestParam("personId") String id) {
        return "Fetched parameter using params attribute = " + id;
    }
}

处理动态 URI

public class IndexController {
    @RequestMapping(value = "/fetch/{id}")
    String getDynamicUriValue(@PathVariable String id) {
        return "Dynamic URI parameter fetched";
    }

    @RequestMapping(value = "/fetch/{id:\d+}/{name}")
    String getDynamicUriValueRegex(
        @PathVariable("id") int id, @PathVariable("name") String name
    ) {
        return "Dynamic URI parameter fetched using regex";
    }
}

默认的处理方法

public class IndexController {
    @RequestMapping()
    String default () {
        return "This is a default method for the class";
    }
}

参考 RequestBody的使用

猜你喜欢

转载自www.cnblogs.com/danhuang/p/12581048.html