我們需要給接口推送數據以及接口接收數據的時候,可以用springmvc中的一種簡單方法
1.需要在spring-mvc.xml中配置信息轉化器。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<bean id = "stringhttpmessageconverter" class = "org.springframework.http.converter.stringhttpmessageconverter" /> <bean id= "jsonhttpmessageconverter" class = "org.springframework.http.converter.json.mappingjacksonhttpmessageconverter" /> <!-- 啟動spring mvc的注解功能,完成請求和注解pojo的映射 --> <bean class = "org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter" > <property name= "messageconverters" > <list> <ref bean= "mappingjacksonhttpmessageconverter" /> <!-- json轉換器 --> <ref bean= "stringhttpmessageconverter" /> <ref bean= "jsonhttpmessageconverter" /> </list> </property> </bean> |
2.需要導入的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.后臺推送數據
1
2
3
4
5
6
7
|
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.接收數據
1
2
3
4
5
6
|
@requestmapping ( "test1" ) @responsebody public map test1(httpservletrequest request, @requestbody jsonobject requestbody) throws ioexception{ system.out.println( "jinru=================" +requestbody); return null ; } |
以上這篇springmvc中resttemplate傳值接值方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/huxiangen/article/details/65627977