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

  • 相关阅读:
    实时获取管道信息的一个小框架
    multiprocessing还是threading?
    QThread的一些使用心得
    super超类继承特点小结
    打靶总结
    简析Colorspace
    第一个Unity3D脚本
    一个新的计划,写在年末
    lambda函数的特性
    Nuke Python module的使用
  • 原文地址:https://www.cnblogs.com/liufei1983/p/9043570.html
Copyright © 2011-2022 走看看