要求:如何在程序中调用外部API接口以获取程序所需的对象
解决方法:使用RestTemplate.exchange ()方法
代码如下。
@postmapping(/test ) ) responsebodypublicstringtest ) requestparammapstring,Object params ) resttemplateresttemplate //headers.add (内容类型,)应用程序/JSON ); httpentityhttpentity=newhttpentity (params,headers ); rest template.getmessage converters ().set (1,newstringhttpmessageconverter ) standardcharsets.utf_8) ); //HttpMethod.Get为get请求,httpEntity为nullreturnresttemplate.exchange (' http://localhost 3360 xxxx/xx/week/lll