Do you have questions ? We got the answers!

Ask a question:

0
How to Use Spring RESTTemplate to Post Data to a Web Service?

Open 1 Answers 79 Views Programming
I have a scenario where I need to send data using RESTTemplate. How do you post data?

1 Answer

0
Sample user class:

public class User

{

    private String user;

    private String name;

    public String getUser()

    {

        return user;

    }

    public void setUser(String user)

    {

        this.user = user;

    }

    public String getName()

    {

        return name;

    }

    public void setName(String name)

    {

        this.name = name;

    }

}

Time for the Client Code

public class Main

{

    public static void main(String[] args) throws IOException

    {

        /**

         *

         * This is setting the REST server configuration in the applicationContext

         *

         */

        ApplicationContext context = new AnnotationConfigApplicationContext(RESTConfiguration.class);

        /**

         *

         * We now get a RESTServer bean from the ApplicationContext which has all the data we need to

         * log into the REST service with.

         *

         */

        RESTServer mRESTServer = context.getBean(RESTServer.class);

        /**

         *

         * Setting up data to be sent to REST service

         *

         */

        Map<String, String> vars = new HashMap<String, String>();

        vars.put("id", "JS01");

        try

        {

            /*

                Code to post and return a user object

             */

            RestTemplate rt = new RestTemplate();

            rt.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

            rt.getMessageConverters().add(new StringHttpMessageConverter());

            String uri = new String("http://" + mRESTServer.getHost() + ":8080/springmvc-resttemplate-test/api/{id}");

            User u = new User();

            u.setName("M Shah");

            u.setUser("MS01");

            User returns = rt.postForObject(uri, u, User.class, vars);

        }

        catch (HttpClientErrorException e)

        {

            ObjectMapper mapper = new ObjectMapper();

            ErrorHolder eh = mapper.readValue(e.getResponseBodyAsString(), ErrorHolder.class);

        }

        catch(Exception e)

        {

        }

    }

}

Comment below if you have any questions/concern.
answered May 18, 2016 by stbadmin (4,970 points)

Top Contributors

  • 4970 pointsstbadmin

    34 questions 454 answers218 comments

  • 433 pointsSunilBhaskar

    41 questions 15 answers32 comments

  • 425 pointsMehedi

    36 questions 3 answers14 comments

  • 364 pointssumitkumarwatts

    0 questions 14 answers0 comments

  • 354 pointsSoft Tester

    43 questions 0 answers4 comments

  • 327 pointsKeeshaGolden

    0 questions 17 answers2 comments

  • 322 pointsVipul Sharma

    44 questions 0 answers24 comments

  • 310 pointsvysh.sharma

    25 questions 0 answers0 comments

  • 267 pointsOnkar_Nimje

    33 questions 15 answers15 comments

  • 258 pointsVanitha

    26 questions 0 answers6 comments

...