springMVC中RestTemplate傳值接值方法
我們需要給接口推送數(shù)據(jù)以及接口接收數(shù)據(jù)的時(shí)候,可以用springmvc中的一種簡(jiǎn)單方法
1.需要在spring-mvc.xml中配置信息轉(zhuǎn)化器。
<bean id = "stringHttpMessageConverter" class = "org.springframework.http.converter.StringHttpMessageConverter"/> <bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> <!-- 啟動(dòng)Spring MVC的注解功能,完成請(qǐng)求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /> <!-- json轉(zhuǎn)換器 --> <ref bean= "stringHttpMessageConverter" /> <ref bean= "jsonHttpMessageConverter" /> </list> </property> </bean>
2.需要導(dǎo)入的jar包 commons-beanutils.jar,commons-httpclient.jar,commons-lang.jar,ezmorph-1.0.5.jar,json-lib-2.2-jdk15.jar,morph-1.1.1.jar
3.后臺(tái)推送數(shù)據(jù)
public static void main(String[] args) { Map map=new HashMap(); map.put("name", "王五"); RestTemplate templates = new RestTemplate(); JSONObject jsonObject = JSONObject.fromObject(map); String result = templates.postForObject("url",jsonObject,String.class); }
4.接收數(shù)據(jù)
@RequestMapping("test1") @ResponseBody public Map test1(HttpServletRequest request,@RequestBody JSONObject requestBody) throws IOException{ System.out.println("jinru================="+requestBody); return null; }
以上這篇springMVC中RestTemplate傳值接值方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringMVC的簡(jiǎn)單傳值(實(shí)現(xiàn)代碼)
- springMvc請(qǐng)求的跳轉(zhuǎn)和傳值的方法
- 詳解SpringMVC注解版前臺(tái)向后臺(tái)傳值的兩種方式
- 詳解springmvc 中controller與jsp傳值
- jQuery+SpringMVC中的復(fù)選框選擇與傳值實(shí)例
- java-jsp springmvc-controller 傳值到頁(yè)面的方法
- springmvc前臺(tái)向后臺(tái)傳值幾種方式總結(jié)(從簡(jiǎn)單到復(fù)雜)
- SpringMVC的ModelAndView傳值方法
- springMVC向Controller傳值出現(xiàn)中文亂碼的解決方案
- SpringMVC前后端傳值的幾種實(shí)現(xiàn)方式
相關(guān)文章
SpringBoot中的聲明式事務(wù)+切面事務(wù)+編程式事務(wù)詳解
這篇文章主要介紹了SpringBoot中的聲明式事務(wù)+切面事務(wù)+編程式事務(wù)詳解,事務(wù)管理對(duì)于企業(yè)應(yīng)用來(lái)說(shuō)是至關(guān)重要的,當(dāng)出現(xiàn)異常情況時(shí),它也可以保證數(shù)據(jù)的一致性,需要的朋友可以參考下2023-08-08java實(shí)現(xiàn)的簡(jiǎn)單猜數(shù)字游戲代碼
這篇文章主要介紹了java實(shí)現(xiàn)的簡(jiǎn)單猜數(shù)字游戲代碼,通過(guò)隨機(jī)數(shù)與邏輯判斷來(lái)實(shí)現(xiàn)游戲功能,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-11-11Spring 實(shí)現(xiàn)自定義監(jiān)聽(tīng)器案例
這篇文章主要介紹了Spring 實(shí)現(xiàn)自定義監(jiān)聽(tīng)器案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01Java通過(guò)MyBatis框架對(duì)MySQL數(shù)據(jù)進(jìn)行增刪查改的基本方法
MyBatis框架由Java的JDBC API進(jìn)一步封裝而來(lái),在操作數(shù)據(jù)庫(kù)方面效果拔群,接下來(lái)我們就一起來(lái)看看Java通過(guò)MyBatis框架對(duì)MySQL數(shù)據(jù)進(jìn)行增刪查改的基本方法:2016-06-06springcloud使用profile實(shí)現(xiàn)多環(huán)境配置方式
這篇文章主要介紹了springcloud使用profile實(shí)現(xiàn)多環(huán)境配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03