Am trying to automate Jira Issue Creation via REST API with Java Rest Assured. The following are my code snippets.
I need to reframe the following JSON in Java and pass it to the Body.
{
"fields": {
"project": {
"id": "13204"
},
"summary": "welcome to testing1",
"issuetype": {
"id": "3"
},
"reporter": {
"name": "parthiban.selvaraj"
},
"priority": {
"id": "3"
},
"description": "description",
"customfield_10201": {
"id": "10608"
}
}
}
Below is my Java code with getter and setter:
package jira;
import com.google.gson.Gson;
import io.restassured.RestAssured;
import io.restassured.authentication.PreemptiveBasicAuthScheme;
import io.restassured.http.ContentType;
import io.restassured.http.Method;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import java.util.List;
public class home {
public static void main (String args[]){
System.out.println("Welcome");
RestAssured.baseURI = "http://##.###.##.##:####/jira/rest/api/2/issue/";
PreemptiveBasicAuthScheme authScheme = new PreemptiveBasicAuthScheme();
authScheme.setUserName("########");
authScheme.setPassword("#######");
RestAssured.authentication = authScheme;
getterSetter values=new getterSetter();
values.setProject(13204);
values.setSummary("Checking via REST Assured");
values.setIssueType(3);
values.setReporter("#######");
values.setPriority(3);
values.setDescription("Welcome to JAVA World");
//Updating sprint name custom field value
values.setCustomfield_10201(10608);
Gson gson=new Gson();
String json= gson.toJson(values);
System.out.println("JSON Values " + json);
RequestSpecification httpRequest = RestAssured.given().header("Content-Type",
"application/json").body(json);
System.out.println(httpRequest + " Request ");
Response response = httpRequest.request(Method.POST, "");
System.out.println(response.getStatusCode());
String responseBody = response.getBody().asString();
System.out.println("response " + responseBody);
JsonPath jsonPath = new JsonPath(responseBody);
}
}
Getter and Setter File:
package jira;
import javax.xml.bind.annotation.XmlRootElement;
//#XmlRootElement
public class getterSetter {
private int project;
private int issueType;
private String reporter;
private String summary;
private int priority;
private String description;
private int customfield_10201;
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public void setProject(int project){
this.project=project;
}
public int getProject() {
return project;
}
public int getIssueType() {
return issueType;
}
public void setIssueType(int issueType) {
this.issueType = issueType;
}
public String getReporter() {
return reporter;
}
public void setReporter(String reporter) {
this.reporter = reporter;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getCustomfield_10201() {
return customfield_10201;
}
public void setCustomfield_10201(int customfield_10201) {
this.customfield_10201 = customfield_10201;
}
}
I understand JSON format which I am passing through request Body is wrong. Can anyone help me to pass correct JSON format in Request Body.
I tested in Postman with the above JSON format Issue created successfully in my Instance.
For the above code am hitting with
response code 500 and Internal Server Error.
According to the JSON string as the payload to create Jira issue, you can directly create several corresponding classes as follows:
class IssueInfo {
private Field fields;
//general getters and setters
}
class Field {
private Project project;
private String summary;
private IssueType issuetype;
private Reporter reporter;
private Priority priority;
private String description;
private Customfield10201 customfield_10201;
//general getters and setters
}
class Project {
private String id;
//general getters and setters
}
class IssueType {
private String id;
//general getters and setters
}
class Reporter {
private String name;
//general getters and setters
}
class Priority {
private String id;
//general getters and setters
}
class Customfield10201 {
private String id;
//general getters and setters
}
After assigning value of each required field, you can pass the instance of IssueInfo as request body.
Related
I am new to Android. When the response is a success, I get a JSON Object and when it is a failure I get an empty JSON array.
I have created a POJO class for the same, but I'm getting the exception below:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:
Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 5 column 14 path
$.data
When the API response is a success, I get the response below:
{
"status": "S",
"code": "000",
"description": "OTP Sent Successfully",
"data": {
"ProcessId": "39a71-6d5c-4ae1-1415e63"
}
}
When the response is a failure, I get the response below:
{
"status": "F",
"code": "002",
"description": "Customer with Mobile already Exists",
"data": []
}
My POJO Class:
public class SendOtpAPI {
#SerializedName("status")
#Expose
private String status;
#SerializedName("code")
#Expose
private String code;
#SerializedName("description")
#Expose
private String description;
#SerializedName("data")
#Nullable
#Expose
private Data data;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
}
POJO Data Class:
public class Data {
#SerializedName("ProcessId")
#Expose
private String processId;
public String getProcessId() {
return processId;
}
public void setProcessId(String processId) {
this.processId = processId;
}
}
Retrofit API Call:
#FormUrlEncoded
#POST("https://dev2.test/otp")
Call<SendOtpAPI> sendOtpRegister(
#Field("operation")String operation,
#Field("mobile")String mobile
);
How can I handle this?
Modify your Model class- the exception itself says the solution
Check your API in Postman your response contains Array I think, And You have declared Data as an object in SendOtpAPI model class. check the below code -
Change Data class into an array in SendOtpAPI model class
private Data data; --> private ArrayList<Data> dataList;
I am pretty new to Java.
I want to model this request for a batch request to Microsoft graphAPI.
{"requests":[
{"id":"employeeId","method":"GET","url":"/me/employeeId"},
{"id":"thumbnailPhoto","method":"GET","url":"/me/photo/$value"}]
}
So "requests" is an array of BatchRequest object.
What I have currently:
// BatchRequest object
public class BatchRequest
{
private String id;
private String method;
private String url;
public BatchRequest(String id, String method, String url)
{
this.id = id;
this.method = method;
this.url = url;
}
// getters and setters below
}
private List<BatchRequest> requests;
#Override
public UserInfoResponse callGraphApi()
{
BatchRequest employeeId = new BatchRequest("employeeId", "GET", "/me/employeeId");
BatchRequest photo = new BatchRequest("thumbnailPhoto", "GET", "/me/photo/$value");
requests.add(employeeId);
requests.add(photo);
return callGraphApi(requests);
}
Is this how I would model the JSON?
Found this Jsonschema2pojo while i was trying to figure out how to model my Json response into java objects in android app development. Install gson or jackson in your project and it'll take care of the things under the hood.
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class BatchRequest {
#SerializedName("requests")
#Expose
private List<Request> requests = null;
public List<Request> getRequests() {
return requests;
}
public void setRequests(List<Request> requests) {
this.requests = requests;
}
}
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Request {
#SerializedName("id")
#Expose
private String id;
#SerializedName("method")
#Expose
private String method;
#SerializedName("url")
#Expose
private String url;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
Sure. The task of turning an instance of a simple java object into a bunch of JSON, as well as the job of turning a bunch of JSON, combined with a list of simple java classes into instances of those classes, is called 'marshalling'. You'll need a library to do it; the popular ones are Jackson and GSON.
I'm having difficulty trying to parse this JSON response into a list of "properties" elements. My JSON looks like this:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"mag": 6.6,
"place": "192km ESE of Tadine, New Caledonia"
}
},
{
"type": "Feature",
"properties": {
"mag": 7.5,
"place": "168km ESE of Tadine, New Caledonia"
}
},
{
"type": "Feature",
"properties": {
"mag": 6,
"place": "155km ESE of Tadine, New Caledonia"
}
}
]
}
This is response contains Earthquake details so basically each "properties" within "features" is the POJO I want, but all of them just in a List. Here is my Earthquake class:
public class Earthquake {
#SerializedName("mag")
private double magnitude;
#SerializedName("place")
private String location;
public Earthquake(double magnitude, String location) {
this.magnitude = magnitude;
this.location = location;
}
// getters
}
I've tried doing custom deserialization suggested here. It gives me the error
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
suggesting that I'm trying to parse a JsonObject instead of a JsonArray. Here is the deserializer I used.
public class EarthquakeDeserializer implements JsonDeserializer<ArrayList<Earthquake>> {
#Override
public ArrayList<Earthquake> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
// get list of "features"
JsonElement features = json.getAsJsonObject().get("features");
JsonArray earthquakeElements = new JsonArray();
for (JsonElement feature : features.getAsJsonArray()){
JsonElement properties = feature.getAsJsonObject().get("properties");
earthquakeElements.add(properties);
}
Type listType = new TypeToken<ArrayList<Earthquake>>(){}.getType();
return new Gson().fromJson(earthquakeElements, listType);
}
}
Any ideas as to what's going on here?
you can create this kind of a POJO class for your Json, No matter if you want just single part of your response body, you need to create POJO for whole response and from that POJO you need to get appropriate attributes. ->
This is your main json object ->
public class Example {
#SerializedName("type")
#Expose
private String type;
#SerializedName("features")
#Expose
private List<Feature> features = null;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<Feature> getFeatures() {
return features;
}
public void setFeatures(List<Feature> features) {
this.features = features;
}
}
this is your feature class ->
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Feature {
#SerializedName("type")
#Expose
private String type;
#SerializedName("properties")
#Expose
private Properties properties;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}
this is your properties class ->
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Properties {
#SerializedName("mag")
#Expose
private Integer mag;
#SerializedName("place")
#Expose
private String place;
public Integer getMag() {
return mag;
}
public void setMag(Integer mag) {
this.mag = mag;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
}
After creating this classes, you can serialize the JSON to POJO via GSON library, you can refer to HussainAbbas's answer for how to do it.
Now you can get anything via creating object of response class, and via that object you can access any property you want. Thanks.
check this out
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
#SerializedName("type")
#Expose
private String type;
#SerializedName("features")
#Expose
private List<Feature> features = null;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<Feature> getFeatures() {
return features;
}
public void setFeatures(List<Feature> features) {
this.features = features;
}
}
-----------------------------------com.example.Feature.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Feature {
#SerializedName("type")
#Expose
private String type;
#SerializedName("properties")
#Expose
private Properties properties;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}
-----------------------------------com.example.Properties.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Properties {
#SerializedName("mag")
#Expose
private Integer mag;
#SerializedName("place")
#Expose
private String place;
public Integer getMag() {
return mag;
}
public void setMag(Integer mag) {
this.mag = mag;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
}
After this add this... in your retrofit response
Gson gson = new Gson()
String data = gson.toJson(response.body());
LoginResponse loginResponse = gson.fromJson(dataString,LoginResponse.class);
Example example = gson.fromJson(dataString,Example.class);
String feature_proprerties_mag = example.getFeatures().getProperties().getMag
I have a below JSON response that I am getting back from a rest service. Now I need to deserialize below JSON response into a POJO. I am working with Jackson.
{
"pagination": {
"number": 1,
"entriesPerPage": 200,
"total": 3
},
"postings": [{
"categories": [{
"taskid": "79720",
"name": "Sunglasses",
"parentCategory": {
"taskid": "394",
"name": "Sunglasses & Fashion Eyewear",
"parentCategory": {
"taskid": "2340",
"name": "Men's Accessories",
"parentCategory": {
"taskid": "10987",
"name": "Clothing, Shoes & Accessories"
}
}
}
}]
},
{
"categories": [{
"taskid": "12980",
"name": "Toys",
"parentCategory": {
"taskid": "123",
"name": "Fashion",
"parentCategory": {
"taskid": "78765",
"name": "Men's Accessories"
}
}
}]
}],
"total": 2
}
In above json, postings is a JSON array which can have multiple posting json object. Now categories is also JSON array. Now tricky part is I can have multiple levels of parentCategory inside each category object and I don't know how many levels of parentCategory I will have. Give above JSON, I need to extract taskid of each category and taskId of last parentCategory. So it should be like this:
79720=10987
12980=78765
Where 79720 is taskId of category and 10987 is the taskId of last parentCategory. Similarly for other one.
Below is my code where I am deserializing JSON into my POJO by making an http call:
ResponseEntity<Stuff> responseEntity = HttpClient.getInstance().getClient()
.exchange(URI.create(endpoint), HttpMethod.POST, requestEntity, Stuff.class);
Stuff response = responseEntity.getBody();
List<Posting> postings = response.getPostings();
for(Posting postings : postings) {
//....
}
Confusion I have is - How to make POJO for above JSON? I tried using jsonschema2pojo but it's not making right classes for parentCategory. Since I can have nested levels of parentCategory which I don't know before hand.
Is this possible to do using Jackson?
This is the POJO class generated for Category and ParentCategory. I am not sure whether I need to make any changes here so that I can parse recursive parentCategory object.
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonPropertyOrder({"taskid", "name", "parentCategory"})
public class Category {
#JsonProperty("taskid")
private String taskid;
#JsonProperty("name")
private String name;
#JsonProperty("parentCategory")
private ParentCategory parentCategory;
#JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
...
}
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonPropertyOrder({"taskid", "name", "parentCategory"})
public class ParentCategory {
#JsonProperty("taskid")
private String taskid;
#JsonProperty("name")
private String name;
#JsonProperty("parentCategory")
private ParentCategory parentCategory;
#JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
...
}
This time I would approach with GSon
Which manages recursion with less effort.
Pojos can be made from json2pojo website, simply choosing GSon as JSon library.
These are the Pojos:
import java.io.Serializable;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Pagination implements Serializable
{
public Pagination() {
super();
// TODO Auto-generated constructor stub
}
#SerializedName("number")
#Expose
private Integer number;
#SerializedName("entriesPerPage")
#Expose
private Integer entriesPerPage;
#SerializedName("total")
#Expose
private Integer total;
private final static long serialVersionUID = 5114620434202813556L;
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public Integer getEntriesPerPage() {
return entriesPerPage;
}
public void setEntriesPerPage(Integer entriesPerPage) {
this.entriesPerPage = entriesPerPage;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
}
import java.io.Serializable;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Categories implements Serializable
{
public Categories() {
super();
// TODO Auto-generated constructor stub
}
#SerializedName("pagination")
#Expose
private Pagination pagination;
#SerializedName("postings")
#Expose
private List<Posting> postings = null;
#SerializedName("total")
#Expose
private Integer total;
private final static long serialVersionUID = 4589512697836725240L;
public Pagination getPagination() {
return pagination;
}
public void setPagination(Pagination pagination) {
this.pagination = pagination;
}
public List<Posting> getPostings() {
return postings;
}
public void setPostings(List<Posting> postings) {
this.postings = postings;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
}
import java.io.Serializable;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Category implements Serializable
{
public Category() {
super();
// TODO Auto-generated constructor stub
}
#SerializedName("taskid")
#Expose
private String taskid;
#SerializedName("name")
#Expose
private String name;
#SerializedName("parentCategory")
#Expose
private ParentCategory parentCategory;
private final static long serialVersionUID = -2127963072268572959L;
public String getTaskid() {
return taskid;
}
public void setTaskid(String taskid) {
this.taskid = taskid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ParentCategory getParentCategory() {
return parentCategory;
}
public void setParentCategory(ParentCategory parentCategory) {
this.parentCategory = parentCategory;
}
}
import java.io.Serializable;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Posting implements Serializable
{
#SerializedName("categories")
#Expose
private List<Category> categories = null;
private final static long serialVersionUID = 8135185675909461065L;
public List<Category> getCategories() {
return categories;
}
public Posting() {
super();
// TODO Auto-generated constructor stub
}
public void setCategories(List<Category> categories) {
this.categories = categories;
}
}
import java.io.Serializable;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class ParentCategory implements Serializable
{
#SerializedName("taskid")
#Expose
private String taskid;
#SerializedName("name")
#Expose
private String name;
#SerializedName("parentCategory")
#Expose
private List<ParentCategory> parentCategory;
public ParentCategory() {
super();
// TODO Auto-generated constructor stub
}
private final static long serialVersionUID = -5989749742502713615L;
public String getTaskid() {
return taskid;
}
public void setTaskid(String taskid) {
this.taskid = taskid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<ParentCategory> getParentCategory() {
return parentCategory;
}
public void setParentCategory(List<ParentCategory> parentCategory) {
this.parentCategory = parentCategory;
}
}
So this is a test implementation:
Pagination pagination = new Pagination();
pagination.setNumber(1);
pagination.setEntriesPerPage(200);
pagination.setTotal(3);
Categories categories = new Categories();
categories.setPagination(pagination);
Category category = new Category();
category.setName("Sunglasses");
category.setTaskid("79720");
List<Category> categoryList = new ArrayList<Category>();
Posting posting = new Posting();
posting.setCategories(categoryList);
List<ParentCategory> parentCategoryList = new ArrayList<ParentCategory>();
List<ParentCategory> parentCategoryList2 = new ArrayList<ParentCategory>();
ParentCategory parentCategory1 = new ParentCategory();
parentCategory1.setName("Sunglasses & Fashion Eyewear");
parentCategory1.setTaskid("394");
ParentCategory parentCategory2 = new ParentCategory();
parentCategory2.setName("Men's Accessories");
parentCategory2.setTaskid("2340");
ParentCategory parentCategory3 = new ParentCategory();
parentCategory3.setName("Clothing, Shoes & Accessories");
parentCategory3.setTaskid("10987");
parentCategoryList.add(parentCategory2);
parentCategoryList2.add(parentCategory3);
parentCategory2.setParentCategory(parentCategoryList2);
parentCategory1.setParentCategory(parentCategoryList);
category.setParentCategory(parentCategory1);
Gson gson = new Gson();
System.out.println(gson.toJson(category));
...and this is the resulting Json:
{
"taskid": "79720",
"name": "Sunglasses",
"parentCategory": {
"taskid": "394",
"name": "Sunglasses \u0026 Fashion Eyewear",
"parentCategory": [{
"taskid": "2340",
"name": "Men\u0027s Accessories",
"parentCategory": [{
"taskid": "10987",
"name": "Clothing, Shoes \u0026 Accessories"
}]
}]
}
}
Maybe still need a bit of tweaking but you got the idea.
Hope it helps!
EDIT: as the op requests I add the Jackson Version.
Pojos:
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonPropertyOrder({"taskid", "name", "parentCategory"})
public class ParentCategory implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
#JsonProperty("taskid")
private String taskid;
#JsonProperty("name")
private String name;
#JsonProperty("parentCategory")
private ParentCategory parentCategory;
public String getTaskid() {
return taskid;
}
public void setTaskid(String taskid) {
this.taskid = taskid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ParentCategory getParentCategory() {
return parentCategory;
}
public void setParentCategory(ParentCategory parentCategory) {
this.parentCategory = parentCategory;
}
}
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonPropertyOrder({"taskid", "name", "parentCategory"})
public class Category implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
#JsonProperty("taskid")
private String taskid;
#JsonProperty("name")
private String name;
#JsonProperty("parentCategory")
private ParentCategory parentCategory;
public String getTaskid() {
return taskid;
}
public void setTaskid(String taskid) {
this.taskid = taskid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ParentCategory getParentCategory() {
return parentCategory;
}
public void setParentCategory(ParentCategory parentCategory) {
this.parentCategory = parentCategory;
}
}
public class Test {
public static void main(String[] args) throws JsonProcessingException {
Category category = new Category();
category.setName("Sunglasses");
category.setTaskid("79720");
List<Category> categoryList = new ArrayList<Category>();
List<ParentCategory> parentCategoryList = new ArrayList<ParentCategory>();
List<ParentCategory> parentCategoryList2 = new ArrayList<ParentCategory>();
ParentCategory parentCategory1 = new ParentCategory();
parentCategory1.setName("Sunglasses & Fashion Eyewear");
parentCategory1.setTaskid("394");
ParentCategory parentCategory2 = new ParentCategory();
parentCategory2.setName("Men's Accessories");
parentCategory2.setTaskid("2340");
ParentCategory parentCategory3 = new ParentCategory();
parentCategory3.setName("Clothing, Shoes & Accessories");
parentCategory3.setTaskid("10987");
parentCategory1.setParentCategory(parentCategory2);
parentCategory2.setParentCategory(parentCategory3);
category.setParentCategory(parentCategory1);
ObjectMapper objectMapper = new ObjectMapper();
String testJson = objectMapper.writeValueAsString(category);
System.out.println(testJson);
}
}
And here again a test result:
{
"taskid": "79720",
"name": "Sunglasses",
"parentCategory": {
"taskid": "394",
"name": "Sunglasses & Fashion Eyewear",
"parentCategory": {
"taskid": "2340",
"name": "Men's Accessories",
"parentCategory": {
"taskid": "10987",
"name": "Clothing, Shoes & Accessories"
}
}
}
}
JSON String
{
"order":{
"address":{
"city":"seattle"
},
"orderItem":[
{
"itemId":"lkasj",
"count":2
},
{
"itemId":"ldka",
"count":3
}
]
}
}
Order Class
public class Order {
private OrderItem[] orderItems;
private CustomerAddress address;
Order(OrderItem[] orderItems, CustomerAddress address ) {
this.orderItems = orderItems;
this.address = address;
}
public OrderItem[] getOrderItems() {
return orderItems;
}
public void setOrderItems(OrderItem[] orderItems) {
this.orderItems = orderItems;
}
public CustomerAddress getAddress() {
return address;
}
public void setAddress(CustomerAddress address) {
this.address = address;
}
}
My OrderItem class
package com.cbd.backend.model;
import org.springframework.data.annotation.Id;
public class OrderItem {
#Id
private String id;
private String itemId;
private String count;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
unit Test that blows up
public String getItemId() {
return itemId;
}
public void setItemId(String itemId) {
this.itemId = itemId;
}
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
}
Unit test to demonstrate issue
package com.cbd.backend.model;
import com.google.gson.Gson;
import org.junit.Test;
import static org.junit.Assert.*;
public class OrderTest {
Gson gson = new Gson();
#Test
public void gsonToOrder() {
Order order = gson.fromJson( a, Order.class );
assertNotNull(order);
assertNotNull(order.getOrderItems()[0]);
}
private final String a = "{ \"order\": { \"address\": { \"city\": \"seattle\" },\"orderItem\":[{ \"itemId\":\"lkasj\", \"count\":2 }, { \"itemId\":\"ldka\", \"count\":3 } ] } }";
}
Should I be using something other than gson or am i constructing this incorrectly
There are two problems in your code:
The root element of your JSON is "order", but the class does not have a property with this name. Try changing you model or just removing the element from the JSON.
There is a mismatch in the name of the "orderItem" property. It is plural in the class, but singular in the JSON.
To sum it up, the following JSON will work without any changes to the code.
{
"address":{
"city":"seattle"
},
"orderItems":[
{
"itemId":"lkasj",
"count":2
},
{
"itemId":"ldka",
"count":3
}
]
}
Also, "count" as it appears in the JSON seems to be numeric, so you might want to change the type of OrderItem.count to int or java.lang.Integer.