我在 Java 中使用 com.fasterxml.jackson.annotation.JsonProperty 和 RestAssured 库。我也使用一些反射方法 我的 JSON 课程
@Getter
@Setter
public class JsonExample {
@JsonProperty("action_flag")
public String actionFlag;
@JsonProperty("first_name")
public String firstName;
@JsonProperty("middle_name")
public String middleName;
@JsonProperty("last_name")
public String lastName;
public JsonExample(String actionFlag, String firstName, String middleName, String lastName) {
this.actionFlag = actionFlag;
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
}
public List<Field> getJsonFields() {
return Stream.concat(Arrays.stream(this.getClass().getDeclaredFields()),
Arrays.stream(this.getClass().getSuperclass().getFields()))
.collect(Collectors.toList()).stream().filter(it -> it.getAnnotation(JsonProperty.class) != null)
.collect(Collectors.toList());
}
public String getFieldValue(String fieldName) {
try {
return getJsonFields().stream()
.filter(it -> it.getAnnotation(JsonProperty.class).value().equals(fieldName)).findFirst()
.orElseThrow(IllegalArgumentException::new).get(this).toString();
} catch (IllegalAccessException e) {
throw new IllegalArgumentException(e.getMessage());
}
}
public void setFieldValue(String fieldName, String value) {
try {
getJsonFields().stream()
.filter(it -> it.getAnnotation(JsonProperty.class).value().equals(fieldName)).findFirst()
.orElseThrow(() ->
new IllegalArgumentException(""))
.set(this, value);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException(e.getMessage());
}
}
}
在日志中使用 post 方法我看到有效载荷
public class Main {
public static void main(String[] args) {
JsonExample json = new JsonExample("1", "Anna", "Del", "Rocco");
RequestSpecification spec = new RequestSpecBuilder()
.log(LogDetail.ALL)
.build();
Response response = given()
.spec(spec)
.body(json)
.when()
.post("https://api.publicapis.org/entries");
}
}
{
"jsonFields": [
{
"name": "actionFlag",
"type": "java.lang.String",
"modifiers": 1,
"annotations": [
{}
],
"declaredAnnotations": [
{}
],
"synthetic": false,
"declaringClass": "ph.onlineloans.qa.json.applications.JsonExample",
"accessible": false,
"enumConstant": false,
"genericType": "java.lang.String",
"annotatedType": {
"type": "java.lang.String",
"annotations": [],
"declaredAnnotations": [],
"annotatedOwnerType": null
}
},
{
"name": "firstName",
"type": "java.lang.String",
"modifiers": 1,
"annotations": [
{}
],
"declaredAnnotations": [
{}
],
"synthetic": false,
"declaringClass": "ph.onlineloans.qa.json.applications.JsonExample",
"accessible": false,
"enumConstant": false,
"genericType": "java.lang.String",
"annotatedType": {
"type": "java.lang.String",
"annotations": [],
"declaredAnnotations": [],
"annotatedOwnerType": null
}
},
{
"name": "middleName",
"type": "java.lang.String",
"modifiers": 1,
"annotations": [
{}
],
"declaredAnnotations": [
{}
],
"synthetic": false,
"declaringClass": "ph.onlineloans.qa.json.applications.JsonExample",
"accessible": false,
"enumConstant": false,
"genericType": "java.lang.String",
"annotatedType": {
"type": "java.lang.String",
"annotations": [],
"declaredAnnotations": [],
"annotatedOwnerType": null
}
},
{
"name": "lastName",
"type": "java.lang.String",
"modifiers": 1,
"annotations": [
{}
],
"declaredAnnotations": [
{}
],
"synthetic": false,
"declaringClass": "ph.onlineloans.qa.json.applications.JsonExample",
"accessible": false,
"enumConstant": false,
"genericType": "java.lang.String",
"annotatedType": {
"type": "java.lang.String",
"annotations": [],
"declaredAnnotations": [],
"annotatedOwnerType": null
}
}
],
"action_flag": "1",
"first_name": "Anna",
"middle_name": "Del",
"last_name": "Rocco"
}
但是我需要像 {"action_flag":"1","first_name":"Anna","middle_name":"Del","last_name":"Rocco"} 这样的有效载荷,没有额外的字段 { “jsonFields”:[ { "name": "动作标志", "type": "java.lang.String", “修饰符”:1, “注释”:[ {} ], “声明注释”:[ {} ],
有人能解释为什么会出现这个字段吗?我怎样才能从 json 对象中删除它?