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";
        }

  • 相关阅读:
    P3916 图的遍历 题解
    NBL小可爱纪念赛「 第一弹 」 游记(部分题解)
    P4147 玉蟾宫 题解
    十、一些小例子
    九、基础正则表达式BRE
    八.linux系统文件属性知识
    七、linux目录结构知识---实战
    六、linux目录结构知识
    3.20-30岁形成好的习惯
    五、Centos linux系统优化-实战
  • 原文地址:https://www.cnblogs.com/liufei1983/p/9043570.html
Copyright © 2011-2022 走看看