远程接口调用中 spring的RestTemplate模板类使用
@Test
public void testGet ( ) {
String path= "http://api.tianapi.com/txapi/saylove/index?key=db3d22eebbff236d1b3b424ea12c3dd6" ;
RestTemplate t = new RestTemplate ( ) ;
String result = t. getForObject ( path, String. class ) ;
System. out. println ( result) ;
}
@Test
public void testRestUrlGet ( ) {
String path= "http://localhost:8080/user/{0}" ;
RestTemplate t = new RestTemplate ( ) ;
String result = t. getForObject ( path, String. class , 22 ) ;
System. out. println ( result) ;
}
@Test
public void testRestUrlGet2 ( ) {
String path= "http://localhost:8080/user/{uid}" ;
RestTemplate t = new RestTemplate ( ) ;
Map< String, Object> map = new HashMap < > ( ) ;
map. put ( "uid" , 22 ) ;
String result = t. getForObject ( path, String. class , map) ;
System. out. println ( result) ;
}
@Test
public void testPost ( ) {
String path = "http://localhost:8080/user/{0}" ;
RestTemplate t = new RestTemplate ( ) ;
Sysuser u = new Sysuser ( ) ;
u. setUname ( "mnb" ) ;
u. setUpwd ( "mmmm" ) ;
u. setUphone ( "11111" ) ;
String s = t. postForObject ( path, u, String. class ) ;
System. out. println ( s) ;
}
@Test
public void testPut ( ) {
String path = "http://localhost:8080/user" ;
RestTemplate t = new RestTemplate ( ) ;
Sysuser u = new Sysuser ( ) ;
u. setUid ( 23 ) ;
u. setUname ( "mnb2" ) ;
u. setUpwd ( "mmmm2" ) ;
u. setUphone ( "22222" ) ;
t. put ( path, u) ;
}
@Test
public void testDelete ( ) {
String path = "http://localhost:8080/user/{0}" ;
RestTemplate t = new RestTemplate ( ) ;
}