springboot几种传参方式

 

package com.steve.controller;

import com.steve.entity.User;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

@RestController
public class HelloController {
    // 不带参数         http://ip:port/test_no_param
    @RequestMapping("/test_no_param")
    public String testNoParam() {
        return "success";
    }
    // 带有一个参数     http://ip:port/test_param?param=xxx
    @RequestMapping("/test_param")
    public String testParam(@RequestParam String param) {
        return "success, the param is: " + param;
    }
    // 带多个参数      http://ip:port/test_multiple_param?name=xxx&password=zzz
    @RequestMapping("/test_multiple_param")
    public String testMultipleParam(@RequestParam String name, @RequestParam String password) {
        return "success, the name is: " + name + " password is: " + password;
    }
    // map接受参数     http://localhost:8081/test_param_map?key=ss&val=zz
    @RequestMapping("/test_param_map")
    public String testParamMap(@RequestParam Map map) {
        return "success, map: " + map;
    }
    // path参数       http://localhost:8081/test/aas
    @RequestMapping("/test/{path}")
    public String testPath(@PathVariable String path) {
        return "success, path: " + path;
    }
//----------------------- postman客户端插件或者浏览器插件模拟   --------------------------
    // post带一个参数   x-www-form-urlencoded的key-value对方式传参
     //                 raw   json格式传参            key为time
    @PostMapping("/test_post_param")
    public String postParam(@RequestBody String time) {
        return "success, time: " + time;
    }
    // post带Map参数     只能用raw   application/json格式传参   key-value自定义
    @PostMapping("/test_post_map_param")
    public String postMapParam(@RequestBody Map map) {
        return "success, map: " + map;
    }
    // post转实体对象   只能用raw   application/json格式传参   key-value跟实体对应
    @PostMapping("/test_post_entity")
    public String postEntity(@RequestBody User user) {
        return "success, entity " + user;
    }
    //GET     http://localhost:8081/test_get    浏览器模拟
    @RequestMapping(value = "/test_get", method = RequestMethod.GET)
    public String testGetMethod() {
        return "get method";
    }
    @GetMapping("/test_get_method")
    public String testGet() {
        return "get method";
    }
    // POST      http://localhost:8081/test_post     postman模拟post请求
    @RequestMapping(value = "/test_post", method = RequestMethod.POST)
    public String testPostMethod() {
        return "post method";
    }
    @PostMapping("/test_post_method")
    public String testPost() {
        return "post method";
    }
}
public class User {

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值