我希望端点具有XML响应。
因此我将此依赖项添加到了pom.xml中:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.9</version>
</dependency>
我的控制器如下所示:
@RestController
@RequestMapping("/api/test")
public class DemoController {
@GetMapping(produces = APPLICATION_XML_VALUE)
public List<String> findAll() {
return Stream.of("hello", "2").collect(Collectors.toList());
}
}
它产生以下结果:
<List><item>hello</item><item>2</item></List>
然后我尝试对其进行测试。我使用了XmlMapper:
private static final ObjectMapper XML_MAPPER = new XmlMapper();
然后XML_MAPPER.writeValueAsString(Stream.of("hello", "2").collect(Collectors.toList()))
返回另一个结果:
<ArrayList><item>hello</item><item>2</item></ArrayList>
出什么问题了?为什么我会有这样的差异?
MockMvc端点仍返回<List>...
示例:
String expected = XML_MAPPER.writeValueAsString(
Stream.of("hello", "2").collect(Collectors.toList()));
mockMvc
.perform(get("/api/test").accept(APPLICATION_XML_VALUE))
.andExpect(content().xml(expected)) // fails here
.andExpect(status().isOk());