You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在模块化Spring Boot应用中接收Postman的JSON格式表数据?

Spring Boot REST接口接收动态列JSON数据的最优实现方案

问题背景

我想用Spring Boot开发Java REST应用,接收Postman传来的JSON格式表数据,每张表列数不固定,要实现模块化开发。现有Controller、Table DTO、Row DTO,第一次使用Spring Boot和Postman,想了解最优实现方式及数据接收原理。

现有代码如下:

Controller代码

package com.example.restservice.controller;

import com.example.restservice.*;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.atomic.AtomicLong;

@RestController
public class TableCollectionController {

    @GetMapping("/getTable")
    public Table table(){
        Table table = new Table();
        table.setTableName("Name");
        table.setAction("Action");

        List columns = new ArrayList();
        Column column = new Column();
        column.setName("Name");
        column.setValue("Value");
        columns.add(column);
        columns.add(column);
        columns.add(column);
        columns.add(column);
        table.setColumns(columns);
        return table;
        
    }
    @PostMapping("/postTable")
    public Table table(@RequestBody Table table1){
        System.out.println(table1.getTableName());
        System.out.println(table1.getAction());
        System.out.println(table1.getColumns());
        return table1;
    }
}

Table DTO代码

package com.example.restservice;

import lombok.Data;

@Data
public class Table {

     String tableName;
     String action;
     List columns; //object list
}

Row DTO代码

package com.example.restservice;

import lombok.Data;

@Data
public class Row {

    public String name;

    public String value;
}

要接收的JSON格式:

{
    "tablename": "tablesName",
    "action": "ADD",
    "data": [
        {
            "columnName": "column1",
            "Value": "valueName"
        },
        {
            "columnName": "column2",
            "Value": "123-1ad-4bc"
        },
        {
            "columnName": "column3",
            "Value": "valueName"
        }
    ]
}

此前仅能处理单列数据,添加多列时报错,现已通过数组实现多列添加,但想了解更规范的最优方案。


核心解答

一、Spring Boot JSON数据接收原理

Spring Boot默认使用Jackson作为JSON序列化/反序列化工具,当客户端发送Content-Type: application/json的POST请求时:

  1. 框架自动读取请求体中的JSON字符串
  2. 依据@RequestBody注解指定的DTO类,将JSON字段映射到类的属性上
  3. 映射规则:默认支持驼峰转下划线、大小写不敏感匹配,但字段名完全匹配优先级最高;可通过@JsonProperty强制指定映射关系

二、DTO类修正(解决字段映射与类型安全问题)

当前代码存在字段名不匹配、泛型缺失的问题,修正如下:

1. 修正Column DTO(匹配JSON字段)

package com.example.restservice;

import lombok.Data;

@Data
public class Column {
    // 与JSON中的columnName完全匹配
    private String columnName;
    // 与JSON中的Value大小写完全匹配
    private String Value;
}

2. 修正Table DTO(指定泛型+显式字段映射)

package com.example.restservice;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import java.util.List;

@Data
public class Table {
    // 显式指定JSON字段tablename映射到tableName属性
    @JsonProperty("tablename")
    private String tableName;
    private String action;
    // 显式指定JSON字段data映射到columns属性,同时指定泛型确保类型安全
    @JsonProperty("data")
    private List<Column> columns;
}

三、Controller优化(模块化与规范)

  1. 移除无用变量,统一接口前缀实现模块化管理
  2. 替换System.out为SLF4J日志,提升专业性
  3. 预留业务逻辑扩展入口,符合分层架构要求

优化后的Controller:

package com.example.restservice.controller;

import com.example.restservice.Table;
import com.example.restservice.Column;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/tables") // 统一接口前缀,便于模块化管理
public class TableCollectionController {

    @GetMapping("/sample")
    public Table getSampleTable(){
        Table table = new Table();
        table.setTableName("sample_table");
        table.setAction("QUERY");

        List<Column> columns = new ArrayList<>();
        Column col1 = new Column();
        col1.setColumnName("id");
        col1.setValue("1");
        columns.add(col1);

        Column col2 = new Column();
        col2.setColumnName("name");
        col2.setValue("test");
        columns.add(col2);

        table.setColumns(columns);
        return table;
    }

    @PostMapping
    public Table receiveTable(@RequestBody Table table){
        // 用日志打印接收信息,替代System.out
        log.info("接收表名: {}", table.getTableName());
        log.info("操作类型: {}", table.getAction());
        log.info("列数据数量: {}", table.getColumns().size());
        // 此处可调用Service层处理业务逻辑,实现模块化解耦
        return table;
    }
}

四、Postman测试步骤

  1. 请求方法选择POST,URL填写http://localhost:8080/api/tables
  2. 切换到Body标签,选择raw,格式设置为JSON
  3. 粘贴目标JSON数据,点击Send即可完成请求
  4. 查看响应体验证数据是否正确接收

五、模块化开发建议

  1. 分层架构:遵循Controller(请求接收)→ Service(业务处理)→ Repository(数据访问)的分层逻辑,禁止在Controller中编写业务代码
  2. DTO拆分:区分请求DTO与响应DTO,若输入输出格式不同,分开定义更清晰
  3. 全局异常处理:捕获JSON解析、参数校验等异常,返回标准化错误响应
  4. 参数校验:给DTO字段添加@NotBlank、@NotNull等校验注解,配合@Valid触发校验

内容的提问来源于stack exchange,提问作者JOSHUA MCBRATNIE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 04:12:36