使用xml创建restful控制器测试时出错

时间:2017-03-21 19:53:46

标签: xml rest spring-boot spring-web

我尝试使用MockMvc创建一个xml中有效负载的控制器测试, 但是当我运行测试时,会出现以下错误:

org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; [Fatal Error]: 1: 1: Premature end of file.

我的测试方法

@Test
public void newTransaction() throws Exception {

    String payload = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            + "<Transaction>"
                + "<transAmt>20059</transAmt>"
                + "<merchId>687555537877464</merchId>"
                + "<merchNameLoc>Best</merchNameLoc>"
                + "<transDate>12312010</transDate>"
                + "<retCd>0</retCd>"
            + "</Transaction>";

    mockMvc.perform(post("/rtm/transaction")
            .content(payload)
            .accept(MediaType.APPLICATION_XML)
            .contentType(MediaType.APPLICATION_XML))
            .andExpect(xpath("//transactionId").exists())
            .andExpect(status().isCreated());
}

我的控制器

@RequestMapping(value = "/transaction", consumes = MediaType.APPLICATION_XML_VALUE)
@ResponseBody
public ResponseEntity<Transaction> newTransaction(@RequestBody NotificationCommand command){
    return null;
}

1 个答案:

答案 0 :(得分:0)

此MediaType.APPLICATION_XML_VALUE的值是多少?它与application / xml相同吗?