在Java POJO中放置具有嵌套值的json响应
我有一个API POST端点,它返回一个json对象,其结构如下:在Java POJO中放置具有嵌套值的json响应,java,json,gson,cucumber,pojo,Java,Json,Gson,Cucumber,Pojo,我有一个API POST端点,它返回一个json对象,其结构如下: 数据(我发送的原始数据) JsonAPI(端点符合的JSON API版本) 状态(对端点的调用的状态) 消息(用于架构验证错误的情况) 所有这些都使用GSON fromJson()存储在以下POJO中: 如您所见,我当前将jsonapi值存储为一个JsonElement。这意味着在解析json中的值时,字符串值为{“version”:“1.0”} 我的目标是将此值存储为子(?)对象。并且,api值不是SupplierResp
- 数据(我发送的原始数据)
- JsonAPI(端点符合的JSON API版本)
- 状态(对端点的调用的状态)
- 消息(用于架构验证错误的情况)
JsonElement
。这意味着在解析json中的值时,字符串值为{“version”:“1.0”}
我的目标是将此值存储为子(?)对象。并且,api值不是SupplierResponseTest中的JsonElement
,而是对象或枚举。然而,我被困在如何做到这一点
以这种方式存储值的目的是能够对新对象的值执行cucumber验证,该值将是1.0
,而不是解析一堆json{“version”:“1.0”}
我用的黄瓜是:
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" |
好的,我是从中领头的
我创建了一个包含嵌套json元素的新类:
package json.responses;
public class ApiVersionResponseTest {
private String version;
private ApiVersionResponseTest(String version) {
this.version = version;
}
public String getApiVersion() {
return version;
}
}
父类构造函数已更改,以允许将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;
}
}
步骤定义(现在起作用)调用嵌套对象,如下所示:
@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());
}
}
宾果班戈。固定的
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;
}
}
@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());
}
}