Spring uses RestTemplate to simulate the form submission example, resttemplateform

Source: Internet
Author: User

Spring uses RestTemplate to simulate the form submission example, resttemplateform

RestTemplate is a class used to access Web services on the client. Similar to other Spring template classes (such as JdbcTemplate and JmsTemplate), we can also customize the template by providing callback methods and configuring the HttpMessageConverter class. Client operations can be performed using the RestTemplate and HttpMessageConveter classes.

1. Declare the RestTemplate bean

@Beanpublic RestTemplate restTemplate(){   return new RestTemplate();}

2. Simulate call

@ Servicepublic class SmsService {// inject RestTemplate @ Autowired RestTemplate; public String sendMsg (String phoneNum, String text) {// set HttpHeaders headers to new HttpHeaders (); headers. setContentType (MediaType. APPLICATION_FORM_URLENCODED); // set MultiValueMap for the submitted parameter <String, String> p = new LinkedMultiValueMap <> (); p. add ("username", "xxx"); p. add ("password", "yyy"); p. add ("phoneNum", phoneNum); p. add ("content", text); // submit the request HttpEntity <MultiValueMap <String, String> entity = new HttpEntity <MultiValueMap <String, String> (p, headers ); string result = restTemplate. postForObject ("http ://.... ", entity, String. class); return result ;}}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.