Announcement Announcement Module
Collapse
No announcement yet.
restTemplate + POST request + parse the response Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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

    Code:
    http://localhost:8080/customer-ws/Customer/Utils/getAllCustomers
    I need to pass the request, for testing I am using curl and passing my request object as below

    Code:
    <?xml version="1.0" encoding="utf-16"?>
    <GetAllCustomersRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:xsd="http://www.w3.org/2001/XMLSchema" location="1">
    </GetAllCustomersRequest>
    if i use curl, i get below response.

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <GetAllCustomerResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:xsd="http://www.w3.org/2001/XMLSchema" 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>
    </GetAllCustomerResponse>
    below is my spring xml file code snippet

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

    Code:
    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....

    -Ronak.

  • #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?

    Comment


    • #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 ....

      -Ronak

      Comment

      Working...
      X