在 java 中将对象列表转换为 JSON 数组 - Spring 引导 api 测试
Convert list of Objects to JSON Array in java - Spring Boot api testing
当我尝试发出 mockmvc post 请求时,我必须在内容标签中传递对象列表,问题是每次我尝试使用 这种方法传递它时 :
public static String asJsonString(final Object obj) {
try {
final ObjectMapper mapper = new ObjectMapper();
final String jsonContent = mapper.writeValueAsString(obj);
return jsonContent;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
我得到这个错误:
{"status":"NOT_ACCEPTABLE","errors":{"timestamp":"2021-11-29T11:53:11.2020882Z","message":"Wrong message format","details":"JSON parse error: Cannot deserialize instance of `java.util.ArrayList<Compania>` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList<Compania>` out of START_OBJECT token\n at [Source: (PushbackInputStream); line: 1, column: 1]"}}
我为仅保存 1 个对象所做的测试运行良好,但每当我尝试添加列表时它就会中断,这里有我的测试代码。
@Test
void successSavePostCompaniaLista() throws Exception {
Compania c1 = new Compania("Compania1 list",
"name1",
"---",
"---",
null,
null);
Compania c2 = new Compania("Compania2 list",
"name2",
"---",
"---",
null,
null);
List<Compania> companias = List.of(c1,c2);
when(companiaRepository.save(any(Compania.class))).then(returnsFirstArg());
this.mockMvc.perform(
post("/companias/lista")
.header("authorization", "Bearer " + token)
.content(asJsonString(companias)) //<-- Here gives me errors
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isCreated())
.andExpect(jsonPath("$.result[0].success[0]").isNotEmpty())
.andExpect(jsonPath("$.result[0].success[0].name").value(c1.getName()))
.andExpect(jsonPath("$.result[0].success[1].name").value(c2.getName()));
}
有效的与此类似,但仅使用一个对象。
http方向,其他都ok
谢谢!
编辑。这是我要测试的服务
public Map<String, Object> postListCompanias(List<Compania> companias) {
for (int i = 0; i < companias.size(); i++) {
Compania companiaN = companias.get(i);
companiaN.setId(null);
companiaRepository.save(companias.get(i));
System.out.println(companias.get(i));
}
Map<String, Object> mappedResult = Collections.singletonMap(
"result",
List.of(
Collections.singletonMap(
"success",
companias
)
)
);
return mappedResult;
}
这里是 控制器:
@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/lista")
public Map<String, Object> createCompanias(@RequestBody List<Compania> companias) {
return companiaService.postListCompanias(companias);
}
在我看来,问题是您正试图将类似以下 JSON 的内容传递给 content
:
[
{
"compania1_list": "Compania1 list",
"name1": "name1",
"s": "---",
"s1": "---",
"o": null,
"o1": null
},
{
"compania1_list": "Compania2 list",
"name1": "name2",
"s": "---",
"s1": "---",
"o": null,
"o1": null
}
]
这是一个包含 2 个 JSON 个对象的 JSON 数组,而不是一个包含 2 个 JSON 个对象的 JSON 数组的 JSON 对象。我的猜测是 MockHttpServletRequestBuilder.content()
方法不会像这样 JSON。
话虽如此,我将更改您的控制器以接受一个对象而不是一个集合,如下所示:
@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/lista")
public Map<String, Object> createCompanias(@RequestBody CompaniasCreationRequest companiasCreationRequest) {
return companiaService.postListCompanias(companiasCreationRequest.getCompanias());
}
正在CompaniasCreationRequest
如下:
public class CompaniasCreationRequest {
private List<Compania> companias;
public CompaniasCreationRequest(List<Compania> companias) {
this.companias = companias;
}
public List<Compania> getCompanias() {
return companias;
}
public void setCompanias(List<Compania> companias) {
this.companias = companias;
}
}
在您的测试中,这意味着以下变化:
@Test
void successSavePostCompaniaLista() throws Exception {
Compania c1 = new Compania("Compania1 list",
"name1",
"---",
"---",
null,
null);
Compania c2 = new Compania("Compania2 list",
"name2",
"---",
"---",
null,
null);
CompaniasCreationRequest companiasCreationRequest = new CompaniasCreationRequest(List.of(c1,c2));
when(companiaRepository.save(any(Compania.class))).then(returnsFirstArg());
this.mockMvc.perform(
post("/companias/lista")
.header("authorization", "Bearer " + token)
.content(asJsonString(companiasCreationRequest))
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isCreated())
.andExpect(jsonPath("$.result[0].success[0]").isNotEmpty())
.andExpect(jsonPath("$.result[0].success[0].name").value(c1.getName()))
.andExpect(jsonPath("$.result[0].success[1].name").value(c2.getName()));
}
现在您的请求正文将如下所示(这是一种更标准的 JSON 格式):
{
"companias": [
{
"compania1_list": "Compania1 list",
"name1": "name1",
"s": "---",
"s1": "---",
"o": null,
"o1": null
},
{
"compania1_list": "Compania2 list",
"name1": "name2",
"s": "---",
"s1": "---",
"o": null,
"o1": null
}
]
}
当我尝试发出 mockmvc post 请求时,我必须在内容标签中传递对象列表,问题是每次我尝试使用 这种方法传递它时 :
public static String asJsonString(final Object obj) {
try {
final ObjectMapper mapper = new ObjectMapper();
final String jsonContent = mapper.writeValueAsString(obj);
return jsonContent;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
我得到这个错误:
{"status":"NOT_ACCEPTABLE","errors":{"timestamp":"2021-11-29T11:53:11.2020882Z","message":"Wrong message format","details":"JSON parse error: Cannot deserialize instance of `java.util.ArrayList<Compania>` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList<Compania>` out of START_OBJECT token\n at [Source: (PushbackInputStream); line: 1, column: 1]"}}
我为仅保存 1 个对象所做的测试运行良好,但每当我尝试添加列表时它就会中断,这里有我的测试代码。
@Test
void successSavePostCompaniaLista() throws Exception {
Compania c1 = new Compania("Compania1 list",
"name1",
"---",
"---",
null,
null);
Compania c2 = new Compania("Compania2 list",
"name2",
"---",
"---",
null,
null);
List<Compania> companias = List.of(c1,c2);
when(companiaRepository.save(any(Compania.class))).then(returnsFirstArg());
this.mockMvc.perform(
post("/companias/lista")
.header("authorization", "Bearer " + token)
.content(asJsonString(companias)) //<-- Here gives me errors
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isCreated())
.andExpect(jsonPath("$.result[0].success[0]").isNotEmpty())
.andExpect(jsonPath("$.result[0].success[0].name").value(c1.getName()))
.andExpect(jsonPath("$.result[0].success[1].name").value(c2.getName()));
}
有效的与此类似,但仅使用一个对象。
http方向,其他都ok
谢谢!
编辑。这是我要测试的服务
public Map<String, Object> postListCompanias(List<Compania> companias) {
for (int i = 0; i < companias.size(); i++) {
Compania companiaN = companias.get(i);
companiaN.setId(null);
companiaRepository.save(companias.get(i));
System.out.println(companias.get(i));
}
Map<String, Object> mappedResult = Collections.singletonMap(
"result",
List.of(
Collections.singletonMap(
"success",
companias
)
)
);
return mappedResult;
}
这里是 控制器:
@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/lista")
public Map<String, Object> createCompanias(@RequestBody List<Compania> companias) {
return companiaService.postListCompanias(companias);
}
在我看来,问题是您正试图将类似以下 JSON 的内容传递给 content
:
[
{
"compania1_list": "Compania1 list",
"name1": "name1",
"s": "---",
"s1": "---",
"o": null,
"o1": null
},
{
"compania1_list": "Compania2 list",
"name1": "name2",
"s": "---",
"s1": "---",
"o": null,
"o1": null
}
]
这是一个包含 2 个 JSON 个对象的 JSON 数组,而不是一个包含 2 个 JSON 个对象的 JSON 数组的 JSON 对象。我的猜测是 MockHttpServletRequestBuilder.content()
方法不会像这样 JSON。
话虽如此,我将更改您的控制器以接受一个对象而不是一个集合,如下所示:
@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/lista")
public Map<String, Object> createCompanias(@RequestBody CompaniasCreationRequest companiasCreationRequest) {
return companiaService.postListCompanias(companiasCreationRequest.getCompanias());
}
正在CompaniasCreationRequest
如下:
public class CompaniasCreationRequest {
private List<Compania> companias;
public CompaniasCreationRequest(List<Compania> companias) {
this.companias = companias;
}
public List<Compania> getCompanias() {
return companias;
}
public void setCompanias(List<Compania> companias) {
this.companias = companias;
}
}
在您的测试中,这意味着以下变化:
@Test
void successSavePostCompaniaLista() throws Exception {
Compania c1 = new Compania("Compania1 list",
"name1",
"---",
"---",
null,
null);
Compania c2 = new Compania("Compania2 list",
"name2",
"---",
"---",
null,
null);
CompaniasCreationRequest companiasCreationRequest = new CompaniasCreationRequest(List.of(c1,c2));
when(companiaRepository.save(any(Compania.class))).then(returnsFirstArg());
this.mockMvc.perform(
post("/companias/lista")
.header("authorization", "Bearer " + token)
.content(asJsonString(companiasCreationRequest))
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isCreated())
.andExpect(jsonPath("$.result[0].success[0]").isNotEmpty())
.andExpect(jsonPath("$.result[0].success[0].name").value(c1.getName()))
.andExpect(jsonPath("$.result[0].success[1].name").value(c2.getName()));
}
现在您的请求正文将如下所示(这是一种更标准的 JSON 格式):
{
"companias": [
{
"compania1_list": "Compania1 list",
"name1": "name1",
"s": "---",
"s1": "---",
"o": null,
"o1": null
},
{
"compania1_list": "Compania2 list",
"name1": "name2",
"s": "---",
"s1": "---",
"o": null,
"o1": null
}
]
}