[Spring Boot] Controller architecture and for an example of using the request parameters

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.
This link: https://blog.csdn.net/sinat_27933301/article/details/101845181
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";
}

Here Insert Picture Description

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;
}

Here Insert Picture Description

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;
}

Here Insert Picture Description

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;
}

Here Insert Picture Description

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;
}

Here Insert Picture Description

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.
Here Insert Picture Description
Here Insert Picture Description
Console output:

修改用户信息User{account='yunfan', password='123456'}

Guess you like

Origin blog.csdn.net/sinat_27933301/article/details/101845181