@Controller
The default return string.html page content in the templates directory.
Plus @ResponseBody annotation methods, you can return JSON, XML or custom content mediaType
@RestController
SUMMARY direct return, automatically converts the object entity JSON format viewresolver InternalResourceViewResolver ineffective.
@RestController = @Controller + @ResponseBody
@RequestBody
JSON data receiving request body, assigned to the attributes of the entity class setter method.
The json "" => String entity is ""
the json "" => entity Integer, Double is null
json of null => entity is null
@RequestBody can be used in conjunction with @RequestParam (), @ RequestBody can only have at most one, @RequestParam () can have more.
String to receive data
@RequestMapping("/index")
public String indexMapping(@RequestBody String jsonStr) {
return jsonStr;
}
To receive the data object entity
// {"name":"hanmeimei","age":12}
@RequestMapping("/index")
public String indexMapping(@RequestBody User user) {
return user.toString();
}
A complex target entity to receive data
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
The object entity is converted into JSON, XML or custom mediaType contents, and return the HTTP response body
@RequestMapping
Maps the request to the controller, the controller can be used in classes and / or methods.
Single request
@RequestMapping("/home")
public class IndexController {
@RequestMapping("/index")
String indexMapping() {
return "Hello from index mapping.";
}
}
Processing a plurality of requests
@RequestMapping("/home")
public class IndexController {
@RequestMapping(value = {
"/",
"/index",
"/index/*.html",
"/index/**/*.html"
})
String indexMultipleMapping() {
return "Hello from index multiple mapping.";
}
}
Processing request type
The default is HTTP GET type.
@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() {}
Processing request type shortcut
@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() {}
Production and consumption process objects
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";
}
}
Processing the message header
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";
}
}
Processing request parameter
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;
}
}
Handle dynamic 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";
}
}
The default approach
public class IndexController {
@RequestMapping()
String default () {
return "This is a default method for the class";
}
}
Reference RequestBody use