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 belowCode:http://localhost:8080/customer-ws/Customer/Utils/getAllCustomers
if i use curl, i get below response.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>
below is my spring xml file code snippetCode:<?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>
here is curl call from command prompt which generate test-respose.txt with above mentioned xml responce.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>
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>Code:curl -X POST -d @GetAllCustomersRequest.xml -H "Content-Type:text/xml" http://localhost:8080/customer-ws/Customer/Utils/getAllCustomers > test-respose.txt
Thanks for your time and any suggestion, plz let me know if any more information required....
-Ronak.![]()


Reply With Quote
,,,, 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 ??