誉天程序员-SpringMVC回顾:五种接参方式

1、GET查询串传参:
2、RESTFul形式传参(参数少)
3、form表单传参
4、混合传参,查询串+表单
5、终极王者,json传参(参数多)

package com.book.admin.controller;

import com.book.admin.entity.User;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Random;

/**
 * @version v1.0 创建时间:8:52
 * @author: 作者:陈子枢
 * @web CSDN:https://blog.csdn.net/nutony
 * @description 描述:回顾SpringMVC如何接收参数,以及POSTMAN测试。
 */
@RestController
public class ParamController {
    //1、GET查询串传参:
    //请求:http://localhost:6060/user/show?name=chen&age=28
    @GetMapping("/user/show")
    public User show(String name, Integer age){
        User user = new User();
        user.setName(name);
        user.setAge(age);

        return user;
    }

    //2、RESTFul形式传参(参数少)
    //请求:http://localhost:6060/user/add/wang/16
    @GetMapping("/user/add/{name}/{age}")
    public User get(@PathVariable String name, @PathVariable Integer age){
        Integer id = new Random().nextInt(101); //0~100
        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setAge(age);
        user.setCreated(new Date());

        return user;
    }

    //3、form表单传参
    @PostMapping("/user/form")
    public User form(@RequestParam String name, @RequestParam Integer age){
        User user = new User();
        user.setName(name);
        user.setAge(age);

        return user;
    }

    //4、混合传参,查询串+表单
    @PostMapping("/user/mix")
    public String form(@RequestParam String title, @ModelAttribute User user){
        return "{title:'"+title+"',"+user.toString()+"}";
    }

    //5、终极王者,json传参(参数多)
    @PostMapping("/user/add")
    public User get(@RequestBody User user){
        Integer id = new Random().nextInt(101); //0~100
        user.setId(id);
        user.setCreated(new Date());

        return user;
    }
}

你可能感兴趣的:(誉天程序员,ssm,frame,三大框架,java,开发语言)