SpringBoot-RestTemplate配置由于经常需要调⽤restful接⼝,特此记⼀下
1.配置实现类
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(15000);
factory.setReadTimeout(5000);
return factory;
}
}
spring到底是干啥的2.使⽤
@Service
public class TestService {
@Autowired
private RestTemplate restTemplate;
//常规调⽤
public String TestPost(String url) {
//url = "****:****/*******";
ResponseEntity<String> results = hange(url, HttpMethod.POST, null, String.class);
String json = Body();
return json;
}
//传输JSON或者JSON数组,对象多层嵌套
public String reserveCarReserve(String url, *** ***) {
url = "****:****/****";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
Map<String, Object> map = new HashMap<>();
Map<String, Object> map2 = new HashMap<>();
map.put("***", ***);
map.put("***", ***);
map.put("***", ***);
map.put("***", ***);
map2.put("ReserveOrder",map);
HttpEntity<Map<String, Object>> request = new HttpEntity<>(map2, headers);
String json = restTemplate.postForEntity(url, request, String.class).getBody();
return json;
}
}
3.暂时没啥好补充的
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论