1

I am trying to call a rest API using rest-assured but getting "javax.xml.bind.MarshalException" Exception. Below is my code for main class

  @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);

        }

I have defined payLoad method as

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; 
    }

When I am running my registerDevice() method, it is displaying the response but along with getting below exception

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)
RocketRaccoon
  • 2,559
  • 1
  • 21
  • 30
Saurabh Garg
  • 199
  • 7
  • 22
  • Possible duplicate of [Unable to marshal type "java.util.HashMap" while hitting the resource with json](https://stackoverflow.com/questions/36787165/unable-to-marshal-type-java-util-hashmap-while-hitting-the-resource-with-json) ; looks like you need to define a wrapper class – Aaron Jul 21 '17 at 14:08

0 Answers0