rest-assured: How to create correct XML based request from POJO object, it works fine for Json based request

829 views
Skip to first unread message

mohan v

unread,
Jul 21, 2014, 5:11:39 AM7/21/14
to rest-a...@googlegroups.com
Hi experts,
I have earlier used rest-assured for Json based request/response
I am pretty much impressed with it 

Now I am assigned a differet project, where xml based request is involved instead of Json. 

I am  not sure if rest-assured supports xml based request the same way as Json request.
Please correct me if I am wrong




For Json based request validation,
I used to create a Java Pojo object  and convert to Json object ,  used to do something like

Customer customer = new Customer("12390833", "firstname", "lastname",   "countrycode");
RequestSpecBuilder requestSpecBuilder = new RequestSpecBuilder();
ResponseSpecBuilder responseSpecBuilder = new ResponseSpecBuilder();

requestSpecBuilder.setContentType(ContentType.JSON);
requestSpecBuilder.setBody(customer);

given().log().all().spec(requestSpecBuilder.build()).expect().spec(responseSpecBuilder.build()).post("http:example.com/customers/");
This worked fine
==============

Problem:
Now for XML based request, I need to post the following XML body:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE oxip SYSTEM "oxip.dtd">
<oxip version="6.0">
<request>
        <reqUserAuth>
            <user>user1</user>
            <password>password1</password>
        </reqUserAuth>
        <reqCustomerAdd>
            <userName>12345</userName>
            <firstName>f</firstName>
            <lastName>O</lastName>
            <countryCode>GB</countryCode>
          </reqCustomerAdd>
    </request>
</oxip>

What I created is similar POJO object and  and used the following
requestSpecBuilder.setContentType(ContentType.XML);
requestSpecBuilder.setBody(customer);
given().log().all().spec(requestSpecBuilder.build()).expect().spec(responseSpecBuilder.build()).post("http:example.com/customers/");
But the the rest-assured creates the following request

Rest-assured generated request body
=================================

Request path: http:example.com/customers
Proxy: <none>
Request params: <none>
Query params: <none>
Form params: <none>
Path params: <none>
Multiparts: <none>
Headers: Content-Type=application/xml
Cookies: <none>
Body:
<oxip version="6.0">
  <request>
<reqCustomerAdd>
<userName>12345</userName>
<firstName>f</firstName>
<lastName>O</lastName>
<countryCode>GB</countryCode>
</reqCustomerAdd>
    <reqUserAuth>
<password>password123</password>
<user>user1</user>
    </reqUserAuth>
  </request>
</oxip>

However  It seems, the request is failing, because server  needs to have <?xml version="1.0" encoding="UTF-8" ?> , <!DOCTYPE oxip SYSTEM "oxip.dtd">
<oxip version="6.0">
 In short the server expects  request in the following format

Expected Request body: 
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE oxip SYSTEM "oxip.dtd">
<oxip version="6.0">
  <request>
<reqCustomerAdd>
<userName>12345</userName>
<firstName>f</firstName>
<lastName>O</lastName>
<countryCode>GB</countryCode>
</reqCustomerAdd>
    <reqUserAuth>
<password>password123</password>
<user>user1</user>
    </reqUserAuth>
  </request>
</oxip>

Question/Query/Problem
How to tell rest-assured requestSpecBuilder to  add <?xml version="1.0" encoding="UTF-8" ?>  <!DOCTYPE oxip SYSTEM "oxip.dtd"> ,   before XML body generated
from Pojo object from requestSpecBuilder

Thanks 
Mohan

Johan Haleby

unread,
Jul 23, 2014, 2:40:20 PM7/23/14
to rest-a...@googlegroups.com
REST Assured uses JAXB to serialize POJO's to XML. So you can google if you can find out how to do this with JAXB and then use the JAXBObjectMapperFactory to configure the JAXBContext accordingly. This is done by specifying a ObjectMapperConfig, for example:

given().config(RestAssured.config().objectMapperConfig(objectMapperConfig().jaxbObjectMapperFactory(new JAXBObjectMapperFactory(..));

Regards,
/Johan


--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages