好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

springMVC中RestTemplate传值接值方法

我们需要给接口推送数据以及接口接收数据的时候,可以用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

查看更多关于springMVC中RestTemplate传值接值方法的详细内容...

  阅读:40次