przemekost
przemekost

Reputation: 91

Send complex Obj (with Map) by POST body - JSON

I have got object with below structure:

class MyObject{
    private String firstString;
    private BigDecimal bigDecimal;
    private NestedObject nestedObject;
    private Map<String, String> map;
}

And I would like to create a POST call via Postman with below JSON:

{
  "firstString": "first",
  "bigDecimal": 1.2222,
  "nestedObject": {
    "secondString": "second"
  },
  "param1": "paramValue1",
  "param2": "paramValue2",
  "param3": "paramValue3"
}

I tried also:

{
  "firstString": "first",
  "bigDecimal": 1.2222,
  "nestedObject": {
    "secondString": "second"
  },
  "customMap" :{ 
     "param1": "paramValue1",
     "param2": "paramValue2",
     "param3": "paramValue3"
  }
}

I would like to treat param1, param2, param3 as a Map key - value, because the parameters will be unknown (amount of elements could be 1 or 1000) and I would like to parse it as one of the step after receive this body.

NestedObject is an exactly nested object with known structure.

@POST
@Path("/")
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
public String getEntity( MyObject myObject){

  //sth
}

When I do this exactly as above after call I get Object but map variable is a null. How can I solve this problem, do you have any ideas ?

EDIT additional info: @lealceldeiro Below you can find my code, is there any "suspicious" part?

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.HashMap;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
class SearchParamsFromRequest {

    private NestedObject nestedObject;

    private String surname;
    private String type;
    private String userName;

    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<>();

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }
}
    @POST
    @Path("/")
    @Consumes({MediaType.APPLICATION_JSON})
    public String getString (SearchParamsFromRequest searchParameters){

        return "Any String";
    }

Upvotes: 1

Views: 1051

Answers (1)

lealceldeiro
lealceldeiro

Reputation: 14958

You can make use of com.fasterxml.jackson.annotation in order to do that.

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;

public class MyObject {

    private String firstString;
    private BigDecimal bigDecimal;
    private NestedObject nestedObject;
    @JsonIgnore
    private Map<String, Object> map = new HashMap<>();
    // you can change it to Map<String, String> if you know they will always
    // be string - string, be consistent with `setAdditionalProperty`

    @JsonAnyGetter
    public Map<String, Object> getMap() {
        return this.map;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.map.put(name, value);
    }
    // getters and setters omitted
    // REMEMBER to follow the java beans naming conventions
    // for example call the setters and getters such as `setFirstName` and `getFirstName` for the `firstName` attribute
}

This (http://www.jsonschema2pojo.org/) can help you to get your classes generated from sources such as JSONs.

Upvotes: 1

Related Questions