python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Spring 接收参数的方式

SpringMVC和SpringBoot接收参数的几种方式详解

作者:北漂IT民工_程序员_ZG

这篇文章主要介绍了SpringMVC和SpringBoot接收参数的几种方式详解,Spring是分层的JavaSE/EE应用轻量级开源框架,以IoC和AOP为内核,提供了展现层 Spring MVC和持久层Spring JDBC以及业务层事务管理等众多的企业级应用技术,需要的朋友可以参考下

SpringMVC、SpringBoot接收参数

SpringMVC和SpringBoot接收参数的方式是一样一样的。

一、传非json参数

下面代码是get、post请求都支持,不过我们是把参数放到路径上,一半这种情况下都用get请求。

涉及到的注解:

    /**
     * http://localhost:80/add?username=zg&password=123456
     * get post都支持,一半路径传参都用get请求
     * @param username
     * @param password
     */
    @RequestMapping(value = "/add")
    public void add(String username, String password) {
        log.info("打印参数:{}--{}", username, password);
    }
    /**
     * http://localhost:80/add2?username=zg&password=123456
     * get post都支持,一半路径传参都用get请求
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "/add2", name = "haha")
    public void add2(@RequestParam(value = "username") String username, @RequestParam("password") String password) {
        log.info("打印参数:{}--{}", username, password);
    }
    /**
     * http://localhost:80/add3?username=zg&password=123456
     * get post都支持,一半路径传参都用get请求
     * @return
     */
    @RequestMapping(value = "/add3")
    public void add3(User user) {
        log.info("打印参数:{}--{}", user.getUserName(), user.getPassword());
    }
    /**
     * http://localhost:80/add4/zg/123456
     * get post都支持,一半路径传参都用get请求
     * @param username
     * @param password
     */
    @RequestMapping(value = "/add4/{username}/{password}")
    public void add4(@PathVariable String username, @PathVariable String password) {
        log.info("打印参数:{}--{}", username, password);
    }
    /**
     * http://localhost:80/add5?username=zg&password=123456
     * get post都支持,一半路径传参都用get请求
     * @param request
     */
    @RequestMapping(value = "/add5")
    public void add5(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        log.info("打印参数:{}--{}", username, password);
    }

二、传json参数

1、单个实体接收参数

    /**
     * http://localhost/toUser/add6
     * body: {"username":"张刚","password":"123456"}
     * get post都可以
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/add6")
    public void add6(@RequestBody User user) {
        log.info("打印参数:{}--{}", user.getUserName(), user.getPassword());
    }

2、实体嵌套实体接收参数

注解:

实体

@Data
@ToString
public class People {
    private Integer count;
    private String peoplename;
    private Student student;
}
@Data
@ToString
public class Student {
    private String studentname;
}
    /**
     * http://localhost:80/add7
     * body: {"peoplename":"张刚","student":{"studentname":"你好"}}
     * get post都可以, body传参一般都用post
     * @param
     */
    @RequestMapping(value = "/add7")
    public void add7(@RequestBody People people) {
        log.info("打印参数:{}--{}", people.getPeoplename(), people.getStudent().getStudentname());
    }

3、实体嵌套List接收参数

实体

@Data
@ToString
public class People {
    private Integer count;
    private String peoplename;
    private List<Student> student;
}
@Data
@ToString
public class Student {
    private String studentname;
}
    /**
     * http://localhost:80/add8
     * body: {"peoplename":"张刚","student":[{"studentname":"你好"},{"studentname":"很好"}]}
     * get post都可以, body传参一般都用post
     * @param
     */
    @RequestMapping(value = "/add8")
    public void add8(@RequestBody People people) {
        List<Student> student = people.getStudent();
        student.stream().forEach(System.out::println);
        log.info("打印参数:{}--{}", people.getPeoplename());
    }

4、Map接收参数

  /**
     * http://localhost:80/add9
     * body: {"peoplename":"张刚","student":[{"studentname":"你好"},{"studentname":"很好"}]}
     * get post都可以, body传参一般都用post
     *
     * @param
     */
    @RequestMapping(value = "/add9")
    public void add7(@RequestBody Map<String, Object> map) {
        log.info(map.get("peoplename").toString());
        List<Map<String, Object>> studentMapList = (List<Map<String, Object>>) map.get("student");
        studentMapList.stream().forEach(System.out::println);
    }

到此这篇关于SpringMVC和SpringBoot接收参数的几种方式详解的文章就介绍到这了,更多相关Spring 接收参数的方式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文