Announcement Announcement Module
No announcement yet.
Rest Template Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Rest Template

    Here in this code i need to make a post rest call to the tomcat server . I need to send username and password along with the post call.

    exception i am getting is:

    2012-06-05 21:15:34,338 DEBUG [org.springframework.web.client.RestTemplate] - <Created POST request for "http://localhost:8080/cube/rest/saiku/session">
    Exception in thread "main" org.springframework.web.client.RestClientException : Could not write request: no suitable HttpMessageConverter found for request type [org.apache.http.client.entity.UrlEncodedFormEntity] and content type [application/x-www-form-urlencoded]
    at org.springframework.web.client.RestTemplate$HttpEn tityRequestCallback.doWithRequest(RestTemplate.jav a:597)
    at org.springframework.web.client.RestTemplate.doExec ute(
    at org.springframework.web.client.RestTemplate.execut e(
    at org.springframework.web.client.RestTemplate.postFo rEntity(
    at com.informit.articleservice.client.ArticleClient.g etArticle(
    at com.informit.resttemplateexample.RestTemplateExamp le.main(

    I have tried with different httpmessage converters.

    jdk 1.7
    tomcat 7.0.23


    this is the java code.
    @Component( "articleClient" )
    public class ArticleClient
    protected RestTemplate restTemplate;
    public ResponseEntity<String> getArticle() throws UnsupportedEncodingException {
    RestTemplate restTemplate = new RestTemplate();

    List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
    messageConverters.add(new FormHttpMessageConverter());
    HttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
    MappingJacksonHttpMessageConverter jsonConverter = new MappingJacksonHttpMessageConverter();
    restTemplate.setMessageConverters(messageConverter s);

    String url = "http://localhost:8080/cube/rest/saiku/session";

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_ URLENCODED);

    ArrayList<BasicNameValuePair> params= new ArrayList<BasicNameValuePair>();
    params.add(new BasicNameValuePair("username", "admin"));
    params.add(new BasicNameValuePair("password", "admin"));

    HttpEntity req = new HttpEntity(new UrlEncodedFormEntity(params),headers);
    ResponseEntity str= restTemplate.postForEntity(url,req, String.class);
    System.out.println( str.getBody() );
    return str;

    Application context.xml code is

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""

    <context:component-scan base-package="com.informit.articleservice" />

    <bean id="httpClientParams" class="org.apache.commons.httpclient.params.HttpCl ientParams">
    <property name="authenticationPreemptive" value="true"/>
    <property name="connectionManagerClass"
    value="org.apache.commons.httpclient.MultiThreaded HttpConnectionManager"/>
    <bean id="httpClient" class="org.apache.commons.httpclient.HttpClient">
    <constructor-arg ref="httpClientParams"/>
    <bean id="credentials" class="org.apache.commons.httpclient.UsernamePassw ordCredentials">
    <constructor-arg value="admin"/>
    <constructor-arg value="admin"/>
    <bean id="httpClientFactory" class="org.springframework.http.client.CommonsClie ntHttpRequestFactory">
    <constructor-arg ref="httpClient"/>
    <!-- <bean id="restTemplate" class="org.springframework.web.client.RestTemplate "/> -->

    <!-- <bean id="restTemplate" class="org.springframework.web.client.RestTemplate "> -->

    <!-- <property name="messageConverters"> -->
    <!-- <list> -->
    <!-- <bean class="org.springframework.http.converter.xml.Mars hallingHttpMessageConverter"> -->
    <!-- <property name="marshaller" ref="jaxbMarshaller"/> -->
    <!-- <property name="unmarshaller" ref="jaxbMarshaller"/> -->
    <!-- </bean> -->
    <!-- <bean class="org.springframework.http.converter.FormHttp MessageConverter"/> -->
    <!-- </list> -->
    <!-- </property> -->
    <!-- </bean> -->

    <!-- <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshalle r"> -->
    <!-- <property name="classesToBeBound"> -->
    <!-- <list> -->
    <!-- <value></value> -->
    <!-- </list> -->
    <!-- </property> -->
    <!-- </bean> -->

    <bean id="articleClient" class="com.informit.articleservice.client.ArticleC lient">
    <!-- <constructor-arg ref="restTemplate"/>
    <constructor-arg ref="credentials"/> -->

    <!-- <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.Map pingJacksonHttpMessageConverter"/>
    <bean class="org.springframework.web.servlet.mvc.annotat ion.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
    <ref bean="jacksonMessageConverter"/>
    </bean> -->


    Any further questions are welcome. Kindly help me in fixing this issue.
    Last edited by [email protected]; Jun 8th, 2012, 06:12 AM.

  • #2
    hi kovi, have you solved the problem?


    • #4
      Originally posted by [email protected] View Post
      The have gone through different approach using httppost methods using apacheclient...
      I m actually using apache client, jersey and rest template all of them together. I like some of the features of jersey and the original apache client. It is like having best of both worlds. not the best approach maybe. but works for me.


      • #5
        Originally posted by qazjvm View Post
        I m actually using apache client, jersey and rest template all of them together. I like some of the features of jersey and the original apache client. It is like having best of both worlds. not the best approach maybe. but works for me.
        Thanks for the replies qazjvm ..Yes i have used resttemplate and apache client .. Till clearing the sessions i have succeeded, but later tasks like loading a query with post call etc were not working with this approach . Are you working on saiku ?