Spring 处理请求和响应相关的注解
程序员文章站
2022-07-02 10:55:00
@Controller 默认返回 templates 目录下的 string.html 页面内容。 在方法中加上 @ResponseBody 注解,可以返回JSON、XML或自定义mediaType的内容 @RestController 直接返回内容,会自动将对象实体转换为JSON格式,视图解析器 ......
@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"; } }
上一篇: SSM!这就是你要的条条框框!