SpringBoot方法参数

luckyFang 2020年08月15日 257次浏览

获取路径的值

假设业务场景:根据文章ID获取文章内容

http://localhost:8080/article/get/1
http://localhost:8080/article/get/2
http://localhost:8080/article/get/3
...
http://localhost:8080/article/{id}

那后端如何获取ID

@Controller
@RequestMapping("/article")
public class ArticleController {
    @GetMapping("/get/{id}")
    public String getArticleById(@PathVariable int id, Model model) {
        model.addAttribute("msg", id);
        return "index";
    }
}

我们需要通过@PathVariable注解来获取路径中的值。

获取路径中的参数

http://localhost:8080/article/receive?id=1
    @GetMapping("/receive")
    public String getArticleById(String id, Model model){
        model.addAttribute("flag",FLAG_SUCCESS);
        model.addAttribute("msg","param-id:"+id);
        return "index";
    }

对于前端表单的数据,我们可以直接通过方法参数来获取。

通过Bean接收数据

当然我们也可以用Bean来接收对象,方法和上面大致相同

http://localhost:8080/article/bean?id=10&msg=hello
    public static class Bean {
        String id;
        String msg;

        public Bean(String id, String msg) {
            this.id = id;
            this.msg = msg;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }
    }

    @GetMapping("/bean")
    public String getBean(Bean bean, Model model){
        System.out.println(bean.getId());
        System.out.println(bean.getMsg());

        model.addAttribute("flag", FLAG_INFO);
        model.addAttribute("msg", "param-id:" + bean.id);
        return "index";
    }

@ModelAttribute 获取

同样我们也可以使用@ModelAttribute注解来获取

http://localhost:8080/article/model?id=20&msg=hello
   @GetMapping("/model")
    public String getModelAttribute(@ModelAttribute("bean") Bean bean,Model model){
        System.out.println(bean.getId());
        System.out.println(bean.getMsg());

        model.addAttribute("flag", FLAG_DANGER);
        model.addAttribute("msg", "param-id:" + bean.id);
        return "index";
    }

通过HttpServletRequest获取

具体使用方法和我们JSP一样直接获取

http://localhost:8080/article/servlet?id=20
    @GetMapping("/servlet")
    public String getServlet(HttpServletRequest request,Model model){
        String id = request.getParameter("id");
        model.addAttribute("flag", FLAG_WARNING);
        model.addAttribute("msg", "param-id:" + id);
        return "index";
    }

RequestParam绑定参数

http://localhost:8080/article/request?id=20

需要注意的是 如果参数不存在将引发异常

    @GetMapping("/request")
    public String requestParam(@RequestParam("id") String id,Model model){
        model.addAttribute("flag", FLAG_WARNING);
        model.addAttribute("msg", "param-id:" + id);
        return "index";

    }

requestBody 接收JSON数据

http://localhost:8080/article/request?id=20
[
    {
        "id":"1",
        "msg":"tom say hello"
    },
    {
        "id":"2",
        "msg":"jack say hello"
    }
]
    @PostMapping("/uploadJSON")
    public String uploadJSON(@RequestBody List<Bean> body,Model model){
        body.forEach(System.out::println);
        model.addAttribute("flag", FLAG_WARNING);
        model.addAttribute("msg",body.size());
        return "index";
    }

上传文件MultipartFile

 @PostMapping("/uploadFile")
    public String uploadFile(@RequestParam("file") MultipartFile file, Model model) {
        if (file.isEmpty()) {
            model.addAttribute("flag", FLAG_DANGER);
            model.addAttribute("msg", "空文件!");
        }
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file.getOriginalFilename());
            InputStream inputStream = file.getInputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, len);
            }
            inputStream.close();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        model.addAttribute("flag", FLAG_SUCCESS);
        model.addAttribute("msg", "上传成功" + file.getOriginalFilename());
        return "index";
    }
完整代码

package com.lu.pojo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;

@Controller
@RequestMapping("/article")
public class ArticleController {
public static final String FLAG_SUCCESS = "success";
public static final String FLAG_INFO = "info";
public static final String FLAG_WARNING = "warning";
public static final String FLAG_DANGER = "danger";

@GetMapping("/get/{id}")
public String getArticleById(@PathVariable int id, Model model) {
    if (id > 10) {
        model.addAttribute("flag", FLAG_DANGER);
    } else {
        model.addAttribute("msg", id);
        model.addAttribute("flag", FLAG_SUCCESS);
    }
    return "index";
}


@GetMapping("/receive")
public String getArticleById(String id, Model model) {
    model.addAttribute("flag", FLAG_SUCCESS);
    model.addAttribute("msg", "param-id:" + id);
    return "index";
}

public static class Bean {
    String id;
    String msg;

    public Bean(String id, String msg) {
        this.id = id;
        this.msg = msg;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}


@GetMapping("/bean")
public String getBean(Bean bean, Model model) {
    System.out.println(bean.getId());
    System.out.println(bean.getMsg());

    model.addAttribute("flag", FLAG_INFO);
    model.addAttribute("msg", "param-id:" + bean.id);
    return "index";
}


@GetMapping("/model")
public String getModelAttribute(@ModelAttribute("bean") Bean bean, Model model) {
    System.out.println(bean.getId());
    System.out.println(bean.getMsg());

    model.addAttribute("flag", FLAG_DANGER);
    model.addAttribute("msg", "param-id:" + bean.id);
    return "index";
}


@GetMapping("/servlet")
public String getServlet(HttpServletRequest request, Model model) {
    String id = request.getParameter("id");
    model.addAttribute("flag", FLAG_WARNING);
    model.addAttribute("msg", "param-id:" + id);
    return "index";
}


@GetMapping("/request")
public String requestParam(@RequestParam("id") String id, Model model) {
    model.addAttribute("flag", FLAG_WARNING);
    model.addAttribute("msg", "param-id:" + id);
    return "index";

}


@PostMapping("/uploadJSON")
public String uploadJSON(@RequestBody List<Bean> body, Model model) {
    body.forEach(System.out::println);
    model.addAttribute("flag", FLAG_WARNING);
    model.addAttribute("msg", body.size());
    return "index";
}


@PostMapping("/uploadFile")
public String uploadFile(@RequestParam("file") MultipartFile file, Model model) {
    if (file.isEmpty()) {
        model.addAttribute("flag", FLAG_DANGER);
        model.addAttribute("msg", "空文件!");
    }
    try {
        FileOutputStream fileOutputStream = new FileOutputStream(file.getOriginalFilename());
        InputStream inputStream = file.getInputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, len);
        }
        inputStream.close();
        fileOutputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    model.addAttribute("flag", FLAG_SUCCESS);
    model.addAttribute("msg", "上传成功" + file.getOriginalFilename());
    return "index";
}

}