简体   繁体   English

在春季启动时,请求json主体中存在可变键值时处理RequestBody

[英]Handling a RequestBody when there are variable key-value in the request json body in spring boot

I am pretty new to java development but I have developed couple of production ready applications on PHP and Python. 我对Java开发非常陌生,但是我已经在PHP和Python上开发了几个可用于生产的应用程序。 I am developing a REST api using spring boot framework and find it little confusion in terms to handling/parsing the request body. 我正在使用Spring Boot框架开发REST api,发现在处理/解析请求主体方面没有什么混乱。 In the other languages I have worked on, it was much simpler. 用我研究过的其他语言,它要简单得多。

If its in python/php, I need not define all the parameters of the request explicitly to handle the request body. 如果它在python / php中,则无需显式定义请求的所有参数来处理请求主体。 But in java, I have to predefine all the request parameters in a POJO class and MAP it. 但是在Java中,我必须预定义POJO类中的所有请求参数并对其进行MAP。 So for every API endpoint I make, I will have to define all in a Java class as the data layer. 因此,对于我创建的每个API端点,我都必须将Java类中的所有端点都定义为数据层。

But in other languages, I dont need to map anything to an array, in php $_POST holds the data objects. 但是用其他语言,我不需要将任何内容映射到数组,在php $ _POST中保存数据对象。

My question is 我的问题是

I have the following requests 我有以下要求

1.
{
  "category": "product/invoice/event",
  "item_id": "Unique tool identifier id",
  "platforms_id": "1",
  "share_platform_settings": {
    "fb_share_type": "page/profile",
    "fb_share_name": "profilename/pagename",
    "fb_id": "fb_uid/page_id"
  }
}

2.
{
  "category": "product/invoice/event",
  "item_id": "Unique tool identifier id",
  "platforms_id": "1",
  "share_platform_settings": {
    "twitter_username": "page/profile",
    "twitter_user_access_token": "profilename/pagename"
  }
}

I had written a class 我写了一堂课

import com.google.common.base.Objects;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Entity
public class User {

    @Id
    @NotNull
    @Size(max = 64)
    @Column(name = "id", nullable = false, updatable = false)

    private String id;

    private String category;

    private String item_id;

    private String platforms_id;

    private String fb_share_type;

    private String fb_share_name;

    private String fb_id;


    User() {
    }

    public User(final String id, final String category,final String item_id,final String platforms_id,final String fb_share_type,final String fb_share_name,final String fb_id) {
        this.id = id;
        this.category = category;
        this.item_id = item_id;
        this.platforms_id = platforms_id;
        this.fb_share_type = fb_share_type;
        this.fb_share_name = fb_share_name;
        this.fb_id = fb_id;
    }

    public String getId() {
        return id;
    }

    public String getCategory() {
        return category;
    }

    public String getItemId() {
        return item_id;
    }

    public String getFbShareType() {
        return platforms_id;
    }

    public String getFbShareName() {
        return category;
    }

    public String getFbId() {
        return category;
    }

    public String setCategory(String category) {
        return this.category = category;
    }


    public void setId(String id) {
        this.id = id;
    }

    public void setItemId(String item_id) {
        this.item_id = item_id;
    }

    public void setFbShareType(String fb_share_type) {
        this.fb_share_type = fb_share_type;
    }

    public void setFbShareName(String fb_share_name) {
        this.fb_share_name = fb_share_name;
    }

    public void setFbId(String fb_id) {
        this.fb_id = fb_id;
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this)
                .add("id", id)
                .add("item_id", item_id)
                .add("fb_share_type", fb_share_type)
                .add("fb_share_name", fb_share_name)
                .add("fb_id", fb_id)
                .add("category", category)
                .toString();
    }
}

I can map the request to the class using @RequestBody Request request , but I have define the class Request with my request params. 我可以使用@RequestBody Request request将该请求映射到该类,但是我已经用我的请求参数定义了Request类。 But my request params keeps changing, I have a different json request structure 2. What would I do in that case? 但是我的请求参数不断变化,我有一个不同的json请求结构2。在这种情况下我该怎么办? What if if I have n number of different requests on the same API? 如果同一API上有多个不同的请求,该怎么办? Do I need to create classes for each of them? 我需要为它们每个创建类吗? or define all the variables in this class itself? 或定义此类本身中的所有变量? Or is there anyway, I dont need anyclass, is jackson dependency used for that? 还是无论如何,我不需要任何类,是使用杰克逊依赖性吗?

Sorry if this a dump question, I am pretty new to java development and I really appreciate understanding a question like this :P 抱歉,如果这是一个转储问题,我对Java开发来说还很陌生,我非常感谢理解这样的问题:P

As you are using key/value parameters in your JSON, you will need to map it with a similar structure in the Backend so you will need to use a collection of type Map like Map<String, String> share_platform_settings in your Entity. 当您在JSON中使用键/值参数时,您将需要在后端中使用类似的结构对其进行映射,因此您将需要在实体中使用Map类型的集合,例如Map<String, String> share_platform_settings

And your Entity will be like: 您的实体将像:

@Entity
public class User {

    @Id
    @NotNull
    @Size(max = 64)
    @Column(name = "id", nullable = false, updatable = false)
    private String id;

    private String category;
    private String item_id;
    private String platforms_id;
    private Map<String, String> share_platform_settings;

    //Constructors, getters and setters
}

This should work for you. 这应该为您工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 启动 - 验证请求正文 json 密钥 - Spring boot - Validating request body json key Spring 使用@RequestBody 时启动获取请求正文 - Spring Boot get the request body while using @RequestBody 通过HTTP发布请求发送键值参数和JSON消息正文 - Send key-value parametres and json message body throught http post request 当使用“-”连字符或“_”下划线时,Spring Boot 从 YAML 配置加载错误的键值对映射 - Spring Boot Loads wrong key-value pair for map from YAML configureation when '-' hyphen OR '_' underscore is used Spring引导变量/嵌套请求体 - Spring boot variable/nested request body Java Spring Boot + Mysql-当mysql查询结果与任何数据库表相对应的任何模型都不匹配时,返回Json(键值)响应 - Java Spring Boot + Mysql - Return a Json (key-value) response when the mysql query result does not match any model corresponding to any database table 根据 RequestBody 中的 key 区分 Spring Boot PostMapping - Distinguish Spring Boot PostMapping based on key in RequestBody 如何在 spring 引导中将 JSON hasmasp 绑定到 @RequestBody - How to bind JSON hasmasp to the @RequestBody in spring boot 将 RequestBody json 转换为对象 - Spring Boot - Converting a RequestBody json to an Object - Spring Boot Spring 使用 JSON RequestBody 引导保存嵌套实体 - Spring Boot save nested Entity with JSON RequestBody
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM