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.