简体   繁体   English

在Java POJO中将带有嵌套值的json响应放置

[英]Placing a json response with nested value in a Java POJO

I have an API POST endpoint which returns a json object structured like so: 我有一个API POST端点,该端点返回一个结构如下的json对象:

  • Data (the original data I sent up) 数据(我发送的原始数据)
  • JsonAPI (The version of JSON API the endpoint is conforming to) JsonAPI(端点遵循的JSON API版本)
  • Status (The status of the call to the endpoint) 状态(端点呼叫的状态)
  • Message (Used in case of a schema validation error) 消息(在架构验证错误的情况下使用)

All of these are stored in the following POJO using GSON fromJson(): 所有这些都使用来自Json()的GSON存储在以下POJO中:

package json.responses;
import com.google.gson.JsonElement;

public class SupplierResponseTest {

    private StatusResponse status;
    private JsonElement jsonapi;
    private String message;
    private JsonElement data;


    public SupplierResponseTest(StatusResponse status, JsonElement jsonapi) {
        this.status = status;
        this.jsonapi = jsonapi;

    }
    public SupplierResponseTest(StatusResponse status, JsonElement jsonapi, String message) {
        this.status = status;
        this.jsonapi = jsonapi;
        this.message = message;
    }
    public SupplierResponseTest(StatusResponse status, JsonElement jsonapi, JsonElement data) {
        this.status = status;
        this.jsonapi = jsonapi;
        this.data = data;

    }
    public StatusResponse getStatus() {
        return status;
    }
    public JsonElement getJsonapi() {
        return jsonapi;
    }
    public String getMessage() {
        return message;
    }
    public JsonElement getData() {
        return data;
    }

}

As you can see I currently store the jsonapi value as a JsonElement . 如您所见,我当前将jsonapi值存储为JsonElement This means that when I parse the value from the json I end up with the string value being {"version":"1.0"} . 这意味着当我从json解析值时,最终得到的字符串值为{"version":"1.0"}

My aim is to store this value as a sub(?) object. 我的目标是将此值存储为sub(?)对象。 And, rather than the api value being a JsonElement within SupplierResponseTest, for it to be an object or an enum. 而且,api值不是SupplierResponseTest中的JsonElement ,而是它是对象或枚举。 However, I'm stuck as to how to do this. 但是,我对如何执行此操作感到困惑。

The purpose of storing the value this way is to be able to perform cucumber validation on the new object's value which would be 1.0 , rather than parsing a bunch of json {"version":"1.0"} . 以这种方式存储值的目的是能够对新对象的值1.0进行黄瓜验证,而不是解析一堆json {"version":"1.0"}

The cucumber I'm using is: 我用的黄瓜是:

Scenario Outline: Add Supplier Details
    Given the system does not know about any Suppliers
    When the client adds the following <supplier>
    Then the response status is <status>
    And the response has the <jsonapi> version
    And the response has the request <supplier>

        Examples:
        | supplier                  | status    | jsonapi   |
        | "Blue Network Energy LTD" | "SUCCESS" | "1.0"     |

The specific step definition I'm having issues with is: 我遇到的具体步骤定义是:

@Then("^the response has the \"([^\"]*)\" version$")
public void the_response_has_the_version(String arg1) throws Throwable {

    try{            

        //This returns a string of json...
        JsonElement apiVersion = supplierResponse.getJsonapi();

        //The below attempts to assert the two values "1.0" and {"version":"1.0"} 
        Assert.assertEquals(arg1, apiVersion.toString());

    } catch (Exception e){
        System.out.println(e.getMessage());
    }


}

Ok, I took the lead from this question . 好的,我率先提出了这个问题

I created a new class which covers the nested json element: 我创建了一个新类,其中包含嵌套的json元素:

package json.responses;
public class ApiVersionResponseTest { 

    private String version;

    private ApiVersionResponseTest(String version) { 
        this.version = version;
    }


    public String getApiVersion() {
        return version;
    }

}

the parent class constructors were changed to allow the apiversion to be passed and created as a new object: 父类的构造函数已更改为允许apiversion传递并创建为新对象:

package json.responses;
import com.google.gson.JsonElement;

public class SupplierResponseTest {

    private StatusResponseTest status;
    private ApiVersionResponseTest jsonapi; //Relevant change
    private String message;
    private JsonElement data;


    public SupplierResponseTest(StatusResponseTest status, ApiVersionResponseTest jsonapi) {
        this.status = status;
        this.jsonapi = jsonapi;

    }
    public SupplierResponseTest(StatusResponseTest status, ApiVersionResponseTest jsonapi, String message) {
        this.status = status;
        this.jsonapi = jsonapi;
        this.message = message;
    }
    public SupplierResponseTest(StatusResponseTest status, ApiVersionResponseTest jsonapi, JsonElement data) {
        this.status = status;
        this.jsonapi = jsonapi;
        this.data = data;

    }
    public StatusResponseTest getStatus() {
        return status;
    }
    public ApiVersionResponseTest getJsonapi() {
        return jsonapi;
    }
    public String getMessage() {
        return message;
    }
    public JsonElement getData() {
        return data;
    }   
}

And the step definition (now working) calls the nested object like so: 步骤定义(现已生效)将调用嵌套对象,如下所示:

@Then("^the response has the \"([^\"]*)\" version$")
public void the_response_has_the_version(String arg1) throws Throwable {

    try{            

        Assert.assertEquals(arg1, supplierResponse.getJsonapi().getApiVersion());

    } catch (Exception e){
        System.out.println(e.getMessage());
    }


}

Bingo bango. 宾果游戏邦戈 Fixed. 固定。

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

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