zoukankan      html  css  js  c++  java
  • java web 基础 json 和 javaBean转化

    github地址: https://github.com/liufeiSAP/JavaWebStudy

    实体类:

    package com.study.demo.domain;
    
    import com.fasterxml.jackson.annotation.JsonProperty;
    
    import java.util.List;
    
    public class Student {
        @JsonProperty(value="anothername")
        private String name;
        private int age;
        private List<Course> courses;
    
        public String getName() {
            return name;
        }
    
        public int getAge() {
            return age;
        }
    
        public List<Course> getCourses() {
            return courses;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public void setCourses(List<Course> courses) {
            this.courses = courses;
        }
    }

    Controller:

           第一种方法: 使用@RequestBody(推荐),springboot集成了jackson,可以自动把json转成对象;

             (注意:json的key的名字要和实体属性的名字一样(如果不一样要加上@JsonProperty 注解)

                               jackson的功能还是很强大的,本例中实体还嵌套了List, 可以正常解析正确。

      第二种方法:  使用HttpServletRequest, 然后读取流,这个方法可以按照自己的方式进行解析。 

     @RequestMapping(value = "/student", method = RequestMethod.POST)
        public String addStudent(@RequestBody Student record) {
            return "ok";
        }
    
        @RequestMapping(value = "/student1", method = RequestMethod.POST)
        public String addStudent1(HttpServletRequest rquests) throws IOException {
            ServletInputStream aaa = rquests.getInputStream();
    
            return "ok";
        }

  • 相关阅读:
    [python] 类组合与聚合关系
    [python] 伪私有属性,防止变量名冲突
    [vim] 配置文件之常用命令模式
    [VIM] 编辑器---多行注释和取消注释及多行复制和黏贴
    [Visual Studio Code] 执行python
    [C] 编译器codeblocks安装注意
    字符串全排列
    集合全量子集提取
    random函数详解
    Solr常用命令总结
  • 原文地址:https://www.cnblogs.com/liufei1983/p/9043570.html
Copyright © 2011-2022 走看看