Announcement Announcement Module
Collapse
No announcement yet.
RestClientException: Could not extract response Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • RestClientException: Could not extract response

    hallo everyone i'm trying to create a restful client which will receive xml from a server. I'm using the restTemplate.exchange() for the interaction with the server. The problem is that i'm taking a RestClientException and the message which appears says: "Could not extract response: no suitable HttpMessageConverter found for response type [model.Registration] and content type [application/xml]". So i decide to add in the dispatcher servlet the followings lines of code:

    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate ">
    <property name="messageConverters">
    <list>
    <bean class="org.springframework.http.converter.xml.Sour ceHttpMessageConverter"/>
    </list>
    </property>
    </bean>

    and in the controller i declare the restTemplate as following:
    @Autowired
    private RestTemplate restTemplate = new RestTemplate();

    and because this didn't work i used:
    @Inject annotation.
    Both of them doesn't work, so it seems that the problem is something else.
    If anyone of you faced or knows how to solve this problem, please give me
    some instructions. I want directions on what to look.


    P.S.
    I tried some moths ago when i start to work with Spring, to retrieve xml data
    from a server and was succesful.I remember that i didn't add anything special
    just use the JAXB annotations and thats it. Now the situation is different and i
    don't know why.

  • #2
    I might be wrong, but I think you may need to create a class which maps the model.Registration XML into an actual object. A SourceHttpMessageConverter is capable of handling text/xml, application/xml and application/*-xml. Check out the OXM documentation at http://static.springsource.org/sprin.../html/oxm.html. I'm no expert on OXM though. I've used Spring Integration in the past for integrating with a few web services.

    As for your dependency injection, at first glance your @Autowired or @Inject doesn't look correct. You're annotating a field while initializing it at the same time with "= new RestTemplate()". If you're injecting a RestTemplate, you don't need to initialize the variable with a new RestTemplate instance.

    Comment


    • #3
      Taking a second look at your code, I think once you fix the dependency injection by removing the "= new RestTemplate()", it may start working. In your application config, you've declared a RestTemplate and set it's messageConverters to include the SourceHttpMessageConverter. However I think that since you're initializing the variable with a new RestTemplate() in your client, it's ignoring the one in your application context. Like I mentioned before, you either have to use injection or you initialize the variable, but not both. So the one being initialized doesn't have the SourceHttpMessageConverter registered.

      Hope that helps.

      Comment


      • #4
        Originally posted by pgrimard View Post
        Taking a second look at your code, I think once you fix the dependency injection by removing the "= new RestTemplate()", it may start working. In your application config, you've declared a RestTemplate and set it's messageConverters to include the SourceHttpMessageConverter. However I think that since you're initializing the variable with a new RestTemplate() in your client, it's ignoring the one in your application context. Like I mentioned before, you either have to use injection or you initialize the variable, but not both. So the one being initialized doesn't have the SourceHttpMessageConverter registered.

        Hope that helps.

        Thank you pgrimard for your responses. I remove the = new RestTemplate(). But didn't work. Actually i try to create from scratch the project and All went good until the time i decide to broke my program in more than one controller classes. Look what i did. In each one of these new classes i insert at each one of them:

        RestTemplate restTemplate= new RestTemplate();
        In order to retrieve data i'm using the JAXB annotations only(for data binding) and i can retrieve whatever i want. But when i'm trying to execute this line of code in the new class:

        ResponseEntity<Registration> result = restTemplate.exchange("http://www.../ckp/user/{id}",
        HttpMethod.GET, entity, Registration.class, id);
        I'm taking again this exception: RestClientException "Could not extract response: no suitable HttpMessageConverter found for response type [org.client.model.profile.Registration] and content type [application/xml]"

        If i put it back in the previous(one main) class can be executed without problem. Really i still don't understand why. I remove the initialisation and still. Probably i need to declare with a specific way the restTemplate. But i don't know how.Any help please?

        Comment


        • #5
          If you're creating a new RestTemplate() now, instead of using dependency injection, are you registering the the JAXB message convert on the new RestTemplate instance?

          Comment


          • #6
            I solve my problem. I just create a new class. I instantiate there the rest template and i take an object of this class when i want to use the restTemplate. Now i don't take the exception and all seems to work fine. pgrimard thank you for the responses and the interest.
            Cheers,
            Alex

            Comment

            Working...
            X