RestTemplate调⽤第三⽅接⼝实现⽅式stTemplate;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/
**
* 封装RestTemplate  请求⽅式service
*/
@Service("RestTemplateService")
public class RestTemplateService {
@Autowired
private RestTemplate restTemplate;
//url(get请求可拼接路径参数)
public  String  getForObject(String url){
String message = ForObject(url, String.class);
return message;
}
//map传参请求⽅式get
public  String  getForObject(String url,Map params){
String message= ForObject(url, String.class, params);
return message;
}
/*map , token传参请求⽅式get
*1 请求的url,其中{}为参数占位符,分别对应请求参数map中的key.
*2请求⽅法.
*3请求实体,可以封装请求体和请求头.
*4返回体转换的类型,这⾥返回json,将其指定为Map.
*5请求参数map.
*
*/
@SuppressWarnings("rawtypes")
public  ResponseEntity<Map>  getByHeaderMap(String url,String token,Map params){
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", token);
@SuppressWarnings({ "unchecked" })
ResponseEntity<Map> response = hange(
url,
HttpMethod.GET,
new HttpEntity<String>(headers),
Map.class,
params);
return response;springframework包
}
//url(post请求可拼接路径参数)
public  String  postForObject(String url){
ResponseEntity<String> message = restTemplate.postForEntity(url, null, String.class);
Body();
}
//url(请求路径)  entityJson(传递参数json串)  post传参此请求⽅式可⽤于列表条件查询添加修改⽅法(多参数传值)
public  String  postForObject(String url,String entityJson){
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
headers.add("Accept", MediaType.String());
HttpEntity<String> formEntity = new HttpEntity<String>(entityJson, headers);
String result=restTemplate.postForObject(url,formEntity,String.class);
return result;
}
public  String  postForObject(String url,HttpServletRequest request,String entityJson){
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
headers.add("Accept", MediaType.String());
headers.add("token", Header("token"));
headers.add("uid", Header("uid"));
headers.add("userid", Header("userid"));
HttpEntity<String> formEntity = new HttpEntity<String>(entityJson, headers);
String result=restTemplate.postForObject(url,formEntity,String.class);
return result;
}
//url(请求路径+路径传参)
public void delete(String url){
restTemplate.delete(url);
}
}
接⼝配置
stTemplate;
import t.annotation.Bean;
import t.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
/
**
* RestTemplate配置类
*/
@Configuration
public class RestTemplateConfig {
//最好是⽤不注释的⽅法,在注⼊的同时设置连接时间,这种注释的也可以,但是没有设置超时时间/*@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder){
return builder.build();
}*/
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(50000);//单位为ms
factory.setConnectTimeout(50000);//单位为ms
return factory;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。