Results 1 to 5 of 5

Thread: Rest Template

Threaded View

  1. #1

    Default 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(RestTemplate.java:436)
    at org.springframework.web.client.RestTemplate.execut e(RestTemplate.java:401)
    at org.springframework.web.client.RestTemplate.postFo rEntity(RestTemplate.java:302)
    at com.informit.articleservice.client.ArticleClient.g etArticle(ArticleClient.java:73)
    at com.informit.resttemplateexample.RestTemplateExamp le.main(RestTemplateExample.java:20)


    I have tried with different httpmessage converters.

    jdk 1.7
    tomcat 7.0.23
    versions

    <spring.version>3.0.4.RELEASE</spring.version>
    <springsecurity.version>3.0.3.RELEASE</springsecurity.version>
    <resteasy.version>2.0.1.GA</resteasy.version>

    this is the java code.
    @Component( "articleClient" )
    public class ArticleClient
    {
    protected RestTemplate restTemplate;
    @SuppressWarnings("unchecked")
    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();
    messageConverters.add(formHttpMessageConverter);
    messageConverters.add(jsonConverter);
    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(event);
    System.out.println( str.getBody() );
    return str;
    }
    }

    Application context.xml code is

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:oxm="http://www.springframework.org/schema/oxm"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schem...ontext-2.5.xsd
    http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">

    <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>
    <bean id="httpClient" class="org.apache.commons.httpclient.HttpClient">
    <constructor-arg ref="httpClientParams"/>
    </bean>
    <bean id="credentials" class="org.apache.commons.httpclient.UsernamePassw ordCredentials">
    <constructor-arg value="admin"/>
    <constructor-arg value="admin"/>
    </bean>
    <bean id="httpClientFactory" class="org.springframework.http.client.CommonsClie ntHttpRequestFactory">
    <constructor-arg ref="httpClient"/>
    </bean>
    <!-- <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>
    <!-- <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.Map pingJacksonHttpMessageConverter"/>
    <bean class="org.springframework.web.servlet.mvc.annotat ion.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
    <list>
    <ref bean="jacksonMessageConverter"/>
    </list>
    </property>
    </bean> -->


    </beans>

    Any further questions are welcome. Kindly help me in fixing this issue.
    Thanks
    Nagarjun
    Last edited by nagarjun.kovi@gmail.com; Jun 8th, 2012 at 05:12 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •