Announcement Announcement Module
No announcement yet.
restTemplate + POST request + parse the response Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • restTemplate + POST request + parse the response

    Hi All,

    I am using spring restTemplate to access the rest webservice, I am able to access the rest webservice with GET request, like I am able to receive the xml responce and able to parse to java.util.List with my bean class. But I am not able to get responce from POST request, as I guess still i am not much clear how to pass parameter while placing POST request. Can anyone please share the links with good examples of POST request using restTemplate. i searched on net but didn't got any useful stuff.

    Below is scenario, plz let me know your suggestions, which will help out me a lot...

    URL which i need to hit where webservice is hosted

    I need to pass the request, for testing I am using curl and passing my request object as below

    <?xml version="1.0" encoding="utf-16"?>
    <GetAllCustomersRequest xmlns:xsi=""
    	xmlns:xsd="" location="1">
    if i use curl, i get below response.

    <?xml version="1.0" encoding="UTF-8"?>
    <GetAllCustomerResponse xmlns:xsi=""
    	xmlns:xsd="" customerCount="3">
    	<returnResponse returnCode="0"></returnResponse>
    	<customer balance="1.0" custId="Cust-Id0"  custName="CustName0"></customer>
    	<customer balance="5.0" custId="Cust-Id2"  custName="CustName2"></customer>
    	<customer balance="8.0" custId="Cust-Id4"  custName="CustName4"></customer>
    below is my spring xml file code snippet

    	<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    		<property name="messageConverters">
    				<bean id="messageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
    					<property name="marshaller" ref="xstreamMarshaller" />
    					<property name="unmarshaller" ref="xstreamMarshaller" />
    	<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
    		<property name="aliases">
    				<prop key="customer"></prop>
    here is curl call from command prompt which generate test-respose.txt with above mentioned xml responce.

    curl -X POST -d @GetAllCustomersRequest.xml -H "Content-Type:text/xml" http://localhost:8080/customer-ws/Customer/Utils/getAllCustomers > test-respose.txt
    Can any one helpout with basic code snippet, how can I use restTemplate to place POST request for above scenario, and to get the responce in java.util.List<Customer>

    Thanks for your time and any suggestion, plz let me know if any more information required....


  • #2
    It seams like a Post is being mixed with a Get.

    Should you be doing one or the other. Post to make a change & possible retrieve a url to the updated object, And a Get to retrieve the object.

    Or am I missing somthing?


    • #3
      Thanks for your time...

      Yes you are correct it should be in that way, but the rest webservice which i need to access it works with POST request only, and I am developing app to consume it, so I can not change it ,,,, functionality looks the same way it suppose to work with GET request but WS understand only POST request, and my query is what shall I pass as POST request, I observed there is method postForObject, but in my scenario as I have given code sinppets above what shall I pass ??

      Appriciate any hint/suggestion which can help me to move fwd ....