Java POJO API响应是否可以转换为与Map〈String,String>数据类型匹配?

k0pti3hp  于 2022-12-28  发布在  Java
关注(0)|答案(4)|浏览(143)

由于数据类型转换,Assert失败:

public void verify(Map<String, String> theNames) {
List <DemoResponse> DemoResponse = Arrays.asList(BaseService.getResponse().as(DemoResponse.class));

System.out.println(demoResponse);
System.out.println(theNames);

}

参数Map<String, String> theNames正在从cubber特征文件中获取数据,并返回预期输出:

{name=Builder, description=Builder demo, id=ye208bq4-2485-8nd3-10ka-92f4cb91795x}

DemoResponse POJO类返回输出:

DemoResponse(id=ye208bq4-2485-8nd3-10ka-92f4cb91795x, name=Builder, description=Builder demo)

这两个输出显然具有相同的属性,但格式不同,因此它们并不相等
结果,这个比较不平等的Assert通过了:

assertThat(demoResponse).isNotEqualTo(theNames);

但是这个比较平等的Assert失败了:

assertThat(demoResponse).isEqualTo(theName);

现在,我想要实现的是使这两种格式相等,以便失败的Assert能够通过。
这是POJO类

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder

public class DemoResponse{

  private UUID id;
  private String name;
  private String description;

}

为了使输出匹配,我尝试将POJO响应转换为Map,如下所示:

List demoResponse = Arrays.asList( BaseService.getResponse().as(DemoResponse.class));
ObjectMapper mapper = new ObjectMapper();
    TypeReference<List<DemoResponse>> typeReference = new TypeReference<List<DemoResponse>>() {/* */};
List<DemoResponse> res = mapper.convertValue(demoResponse,  typeReference);

System.out.println(res);

输出:

[DemoResponse(id=ye208bq4-2485-8nd3-10ka-92f4cb91795x, name=Builder, description=Builder demo)]

输出与前面的方法略有不同,但仍然与Map<String, String> theNames的格式输出不匹配。
如何使这两个输出的格式匹配呢?我不介意更改其中任何一个的数据类型,只要它们的输出匹配,我就会欣喜若狂。

8oomwypt

8oomwypt1#

要将任何类转换为Map,反之亦然,您可以执行以下操作:

ObjectMapper mapper = new ObjectMapper();
DemoResponse foo = new DemoResponse(UUID.randomUUID(), "name", "description");

// Convert POJO to Map
Map<String, Object> map =
        mapper.convertValue(foo, new TypeReference<>() {
        });

// Convert Map to POJO
DemoResponse anotherFoo = mapper.convertValue(map, DemoResponse.class);

请记住,在类DemoResponse中,必须有一个默认构造函数(没有参数),还可能有getter/setter或用

@JsonProperty

之后,您可以将POJO转换为Map,或者将值的Map转换为POJO并执行所需的Assert。

jm2pwxwz

jm2pwxwz2#

我有一个基本的响应,因为你只有3个字段和相等的匹配类,你只需要用Map〈String,String〉创建一个DemoResponse,也许用一个构造函数。

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder

public class DemoResponse{

  private UUID id;
  private String name;
  private String description;

  public DemoResponse(Map<String, String> theNames){
     this.id = theNames.get("id");
     this.name = theNames.get("name");
     this.description = theName.get("description");
  }
}

在你可以这样Assert之后

assertThat(demoResponse).isEqualTo(new DemoResponse(theName));
2lpgd968

2lpgd9683#

你正在比较列表和Map,它们不是同一个对象,因此它们不相等。你可以尝试先将POJO转换为Map:

ObjectMapper mapper = new ObjectMapper(); 
Map<String, String> demoResponse = mapper.convertValue(BaseService.getResponse(), new TypeReference<Map<String,String>>() {});

那么你可以用这个来检查:

assertThat(demoResponse).isEqualTo(theNames);
q3qa4bjr

q3qa4bjr4#

更改您的行

TypeReference<List<DemoResponse>> typeReference = new TypeReference<List<DemoResponse>>() {/* */};
List<DemoResponse> res = mapper.convertValue(demoResponse,  typeReference);

TypeReference<DemoResponse> typeReference = new TypeReference<DemoResponse>() {/* */};
DemoResponse res = mapper.convertValue(demoResponse,  typeReference);

你会得到你想要的。你把一个值解析成一个列表,因为你的值被[]包围,这就是你的区别

相关问题