我试图使用rest-assured来调用rest API,但是获得了“javax.xml.bind.MarshalException”异常。 下面是我的主类
的代码 @Test
public void registerDevice() throws IOException {
config = new FileInputStream("Configuration.properties");
prop.load(config);
HashMap<String, String> phone = CommonPayLoad.payLoadData("register-device");
RestAssured.baseURI = prop.getProperty("url");
Response registerResponse = given().
body(phone).
when().
post(CommonResource.resgisterDevice()).
then().assertThat().statusCode(200).and().contentType(ContentType.JSON).
extract().response();
String resgisterResponseString = registerResponse.asString();
JsonPath js = new JsonPath(resgisterResponseString);
String _bb_vid = js.get("visitor_id");
System.out.println("response String= "+resgisterResponseString);
}
我已将payLoad方法定义为
public static HashMap<String, String> payLoadData(String api_name) throws IOException{
String apiName = api_name;
HashMap<String, String> payLoad = new HashMap<String, String>();
FileInputStream fis = new FileInputStream("Payload.xls");
HSSFWorkbook workbookExcel = new HSSFWorkbook(fis);
HSSFSheet worksheet = workbookExcel.getSheetAt(0);
int rowNum = worksheet.getLastRowNum();
for (int i=0; i<rowNum; i++) {
HSSFRow rowHeader = worksheet.getRow(0);
HSSFRow row = worksheet.getRow(i+1);
short cellHeaderNum = rowHeader.getLastCellNum();
short cellNum = row.getLastCellNum();
for(int j=0; j<cellHeaderNum-1; j++) {
HSSFCell cellHeader = rowHeader.getCell(j+1);
HSSFCell apiNameCell = row.getCell(0);
HSSFCell cell =row.getCell(j+1);
cell.setCellType(CellType.STRING);
String cellValue = cell.getStringCellValue();
String cellHeaderValue = cellHeader.getStringCellValue();
String apiNameCellValue = apiNameCell.getStringCellValue();
if(apiNameCellValue.equals(apiName)) {
if(!cellValue.equals("NA")) {
payLoad.put(cellHeaderValue, cellValue);
}
}
}
}
workbookExcel.close();
fis.close();
return payLoad;
}
当我运行registerDevice()方法时,它会显示响应,但同时显示异常
javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "java.util.HashMap" as an element because it is missing an @XmlRootElement annotation]
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:311)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:236)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:116)
at javax.xml.bind.Marshaller$marshal$0.call(Unknown Source)