Announcement Announcement Module
Collapse
No announcement yet.
RestTemplate and POST Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • RestTemplate and POST

    Hi,

    I am trying to invoke POST routine on a RESTful service using RestTemplate. Its not working. Below is the code:

    RestTemplate rt = new RestTemplate();
    URI uri = new URI("http://localhost:9000/personservice/person");
    Person p = new Person();
    p.setName("John");
    rt.postForLocation(uri, p);

    When I run the above code, its giving me following error:
    Could not write request: no suitable HttpMessageConverter found for requst type [demo.Person]

    Any clue?

    Raj

  • #2
    Hi,

    You gave RestTemplate an object and asked it to send it over the wire. It doesn't know how. If you have the Jackson JSON library on the classpath, it will try that. If you have JAXB present on the classpath, it will also try that (assuming you've annotated your Person entity with JAXB annotations). The default HttpMessageConverters only handle byte[]s, Strings, XML Source objects, etc.

    Additionally, you can specifically provide an HttpMessageConverter implementation of your own by adding it to the messageConverters list that RestTemplate maintains but I still suggest using the JACKSON or XML approach, since that's convenient.

    Comment


    • #3
      Thanks Josh

      What do we mean by XML approach? Does it mean I have to pass XML string instead of Person object to the postForLocation method?

      Raj

      Comment


      • #4
        Originally posted by rhathi View Post
        Thanks Josh

        What do we mean by XML approach? Does it mean I have to pass XML string instead of Person object to the postForLocation method?

        Raj
        OK I got the solution,
        I had to put @XmlRootElement and it worked like a cream. RestTemplate internally uses Jaxb2RootElementHttpMessageConverter converter to check JAXB objects.

        Thanks Josh.

        Raj

        Comment

        Working...
        X