简体   繁体   中英

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. I am developing a REST api using spring boot framework and find it little confusion in terms to handling/parsing the request body. 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. But in java, I have to predefine all the request parameters in a POJO class and MAP it. So for every API endpoint I make, I will have to define all in a Java class as the data layer.

But in other languages, I dont need to map anything to an array, in php $_POST holds the data objects.

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. But my request params keeps changing, I have a different json request structure 2. What would I do in that case? What if if I have n number of different requests on the same 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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM