Springboot接收请求参数示例

package com.example.demo.controller;

import com.example.demo.model.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/handle")
public class DemoController {
    @RequestMapping
    public String handle(@RequestParam(name = "username", required = false) String name,
                         @RequestParam String password) {
        System.out.println(name);
        System.out.println(password);
        return "finish";
    }

    @RequestMapping("/obj")
    public String handleObj(User user) {
        System.out.println(user);
        return "finish";
    }

    @RequestMapping("/json")
    public String handleJson(@RequestBody User user) {
        System.out.println(user);
        return "finish";
    }

    @RequestMapping("/url/{name}/{id}")
    public String handleUrl(@PathVariable("name") String v_name, @PathVariable Integer id) {
        System.out.println(v_name);
        System.out.println(id);
        return "finish";
    }

    @RequestMapping("/url2/{name}/{id}")
    public String handleUrl2(@PathVariable Map pathMap) {
        System.out.println(pathMap);
        return "finish";
    }

    @RequestMapping("/file")
    public String handleFile(@RequestPart(name = "file") MultipartFile multipartFile) {
        System.out.println("上传文件的文件名:" + multipartFile.getOriginalFilename());
        System.out.println("上传文件的大小:" + multipartFile.getSize());
        System.out.println(multipartFile);
        return "finish";
    }

    @RequestMapping("/header")
    public String handleHeader(@RequestHeader("User-Agent") String userAgent) {
        System.out.println(userAgent);
        return "finish";
    }

    @RequestMapping("/allHeader")
    public String handleAllHeader(@RequestHeader Map headersMap) {
        System.out.println(headersMap);
        return "finish";
    }
}
posted @ 2022-10-04 09:48  xl4ng  阅读(39)  评论(0编辑  收藏  举报