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

  • 相关阅读:
    Linux下安装Mysql
    mssql 查询效率
    查看apache是否安装及版本
    centos(linux)切换用户
    mysql操作命令(linux)
    远程连接MySql连不上1130
    JAVA环境配置
    SQLSERVER2012数据库还原
    ASP连接ACCESS数据库
    ODOO 常用widget
  • 原文地址:https://www.cnblogs.com/liufei1983/p/9043570.html
Copyright © 2020-2023  润新知