默认返回 templates 目录下的 string.html 页面内容。
在方法中加上 @ResponseBody 注解,可以返回JSON、XML或自定义mediaType的内容
直接返回内容,会自动将对象实体转换为JSON格式,视图解析器 InternalResourceViewResolver 不起作用。
@RestController = @Controller + @ResponseBody
接收请求体中的 JSON 数据,通过实体类的setter方法赋值给属性。
json 的 "" => 实体 String 为 ""
json 的 "" => 实体 Integer、Double 为 null
json 的 null => 实体为 null
@RequestBody 可以与 @RequestParam() 同时使用,@RequestBody 最多只能有一个,@RequestParam() 可以有多个。
@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();
}
将对象实体转换为JSON、XML或自定义mediaType的内容,并在 HTTP response body 中返回
将请求映射到控制器上,可以在控制器类和/或方法上使用。
@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"})@ResponseBodyString getProduces() {return "Produces attribute";}// 消费 application/JSON & application/XML 请求@RequestMapping(value = "/cons", consumes = {"application/JSON","application/XML"})@ResponseBodyString 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;}
}
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的使用
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态