Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
annotation | DEFINITIONS |
---|---|
@Controller | Http request processing |
@RestController | Return json, the equivalent of @ Controller + @ ResponseBody |
@RequestMapping | URL mapping configuration |
@GetMapping | Annotations combination, is an abbreviation @RequestMapping (method = RequestMethod.GET) of |
@PostMapping | Annotations combination, is an abbreviation @RequestMapping (method = RequestMethod.POST) of |
@PathVariable | Acquiring data in the URL |
@RequestParam | Gets Request Parameter |
Example 1: Returns a string Json page or jump to (depending on whether there @ResponseBody, using the example of class with annotations @RestController, thus comprising @ResponseBody, it returns Json string)
@RequestMapping(value="/demo/java")
public String java() {
return "java";
}
Example Two: not pass parameters
@RequestMapping(value="/demo/json")
public Map<String, Object> json() {
Map<String, Object> map = new HashMap<>();
map.put("sex", "男");
map.put("name", Arrays.asList("tom","bob","tony"));
return map;
}
Example Three: mass participation
@RequestMapping(value="/demo/getId")
public Map<String, Object> getId(Integer id) {
Map<String, Object> map = new HashMap<>();
map.put("id", id);
return map;
}
Example Four: Parameter settings
name: transmitting a request field mapped to the method parameters.
required: losing settings, default true.
defaultValue: defaults
@RequestMapping(value="/demo/login")
public Map<String, Object> login(
@RequestParam(name = "user", required = false, defaultValue = "admin") String account,
@RequestParam(name = "pass", required = false, defaultValue = "123456") String password) {
Map<String, Object> map = new HashMap<>();
map.put("account", account);
map.put("password", password);
return map;
}
Example Five: passing objects
@RequestMapping(value="/demo/bean")
public Map<String, Object> bean(User user) {
Map<String, Object> map = new HashMap<>();
map.put("account", user.getAccount());
map.put("password", user.getPassword());
return map;
}
Example Six: the argument as part of the URL address
@GetMapping("/user/{id}")
public void getUser(@PathVariable int id) {
System.out.println("查询到了id="+id);
}
Send Request: http: // localhost: 8080 / user / 1
console output:
查询到了id=1
Example seven: Model Data Model
SpringMVC internally using a Model interface memory data model, it functions like a java.util.Map, but easier to use than Map.
@PostMapping("/user")
public void saveUser(User user, Model model) {
System.out.println(model);
System.out.println("添加用户信息 user:"+ user);
}
Send request: http: // localhost: 8080 / user account = yunfan & password = 123456?
Console output:
{user=User{account='yunfan', password='123456'}, org.springframework.validation.BindingResult.user=org.springframework.validation.BeanPropertyBindingResult: 0 errors}
添加用户信息 user:User{account='yunfan', password='123456'}
Example eight: Json request
@PostMapping("/updUser")
public void updateUser(@RequestBody User user) {
System.out.println("修改用户信息"+user);
}
Send Request: http: // localhost: 8080 / updUser
use Json transmission request tool post, the first parameter settings Headers.
Console output:
修改用户信息User{account='yunfan', password='123456'}