当前位置: 移动技术网 > IT编程>开发语言>Java > Spring使用RestTemplate模拟form提交示例

Spring使用RestTemplate模拟form提交示例

2019年07月19日  | 移动技术网IT编程  | 我要评论

百万亚瑟王怎么玩,甄嬛传 快播,艺龙旅游指南

resttemplate是用来在客户端访问web服务的类。和其他的spring中的模板类(如jdbctemplate、jmstemplate)很相似,我们还可以通过提供回调方法和配置httpmessageconverter类来客户化该模板。客户端的操作可以完全使用resttemplate和httpmessageconveter类来执行。

1.声明resttemplate的bean

@bean
public resttemplate resttemplate(){
   return new resttemplate();
}

2.模拟调用

@service
public class smsservice {
   //注入resttemplate
  @autowired
  resttemplate resttemplate;


  public string sendmsg(string phonenum,string text){


    //请求头设置
    httpheaders headers = new httpheaders();
    headers.setcontenttype(mediatype.application_form_urlencoded);


    //提交参数设置
    multivaluemap<string,string> p = new linkedmultivaluemap<>();
    p.add("username","xxx");
    p.add("password","yyy");
    p.add("phonenum",phonenum);
    p.add("content",text);

    //提交请求
    httpentity< multivaluemap<string,string>> entity = new httpentity< multivaluemap<string,string>>(p,headers);

    string result = resttemplate.postforobject("http://....",entity,string.class);

    return result;

  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网