springMVC接受參數(shù)

Control接收客戶端參數(shù)
package firstproject.first.controller;

import firstproject.first.bean.Car;
import firstproject.first.bean.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class HelloController {

    @Autowired
    Car car;

    @RequestMapping("hello")
    public Car hello() {
        log.info(car.toString());
        return car;
    }

    /**
     * 獲取路徑上的參數(shù)、請求頭參數(shù)和?后的參數(shù)(包括單個和全部)
     * localhost:9000//car/1/owner/zhangsan?hobby=BBQ&hobby=lanqiu&age=99
     * **/
    @RequestMapping("/car/{id}/owner/{name}")
    public Map<String, Object> getCar(@PathVariable("id") int id,
                                      @PathVariable("name") String name,
                                      @PathVariable Map<String, Object> pv,
                                      @RequestHeader("Content-Type") String ContentType,
                                      @RequestHeader Map<String, Object> header,
                                      @RequestParam("age") String age,
                                      @RequestParam("hobby") List<String > hobby,
                                      @RequestParam Map<String, Object> params,
                                      @CookieValue(value = "userId", defaultValue = "id") String userId
                                      ) {

        Map<String, Object> map = new HashMap<>();

        map.put("id", id);
        map.put("name", name);
        map.put("pv", pv);
        map.put("ContentType", ContentType);
        map.put("header", header);
        map.put("age", age);
        map.put("hobby", hobby);
        map.put("params", params);
        return map;
    }

    /**接收單個參數(shù)**/
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public Map postMethod(@RequestBody String content,
                          @RequestParam(name = "age", required = false) String age,
                          @RequestParam(name = "name", defaultValue = "lisi") String name) {

        Map<String, Object> map = new HashMap<>();

        map.put("content", content);
        map.put("age", age);
        map.put("name", name);
        return map;
    }

    /**Map 接收所有表單參數(shù)**/
    @RequestMapping(value = "save1", method = RequestMethod.POST)
    public Map postMethod1(@RequestParam Map<String, Object> param) {
        log.info(param.toString());

        Map<String, Object> map = new HashMap<>();

        map.put("age", param.get("name"));
        map.put("name", param.get("age"));
        return map;
    }

    /**對像接收所有表單參數(shù)**/
    @RequestMapping(value = "save2", method = RequestMethod.POST)
    public User postMethod2(User user) {
        return user;
    }

    /**對像接收所有JSON格式參數(shù)**/
    @RequestMapping(value = "save3", method = RequestMethod.POST)
    public User postMethod3(@RequestBody User user) {
        return user;
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容