Open woshikid opened 3 years ago
POM
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.2.8.RELEASE</version> </dependency>
使用RestTemplate
String url = "http://127.0.0.1/rest/api"; // 支持路径参数 RestTemplate restTemplate = new RestTemplate(); // HEAD操作 HttpHeaders headers = restTemplate.headForHeaders(url); MediaType mediaType = headers.getContentType(); // GET/POST操作 ResponseEntity<String> stringEntity = restTemplate.getForEntity(url, String.class); // postForEntity/postForLocation //ResponseEntity<String> stringEntity = restTemplate.exchange(url, HttpMethod.GET, null, String.class); String response = stringEntity.getBody(); //String response = restTemplate.getForObject(url, String.class); // postForObject // 设置HttpMessageConverter restTemplate.getMessageConverters().add(new FastJsonHttpMessageConverter()); //restTemplate.setMessageConverters(List.of(new FastJsonHttpMessageConverter())); ResponseEntity<User> userEntity = restTemplate.getForEntity(url, User.class); User user = userEntity.getBody(); //User user = restTemplate.getForObject(url, User.class);
提交表单
HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); MultiValueMap<String, String> body = new LinkedMultiValueMap<>(); body.add("key", "value"); HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(body, headers); ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, request, String.class);
上传文件
HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); body.add("file", new FileSystemResource("/path/to/file")); InputStream in = new FileInputStream("/path/to/file"); body.add("file", new InputStreamResource(in) { @Override public String getFilename() { return "filename"; } @Override public long contentLength() throws IOException { return in.available(); } }); HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(body, headers); String response = restTemplate.postForObject(url, request, String.class);
设置超时
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); // 使用HttpURLConnection requestFactory.setConnectTimeout(60000); requestFactory.setReadTimeout(60000); RestTemplate restTemplate = new RestTemplate(requestFactory); // 默认即使用SimpleClientHttpRequestFactory
设置拦截器
restTemplate.setInterceptors(List.of(new ClientHttpRequestInterceptor() { @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { System.out.println("before execute"); return execution.execute(request, body); } }));
自动配置类为RestTemplateAutoConfiguration
RestTemplateAutoConfiguration
配置RestTemplate
@Bean public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) { return restTemplateBuilder.build(); // 默认自动检测并设置requestFactory }
POM
使用RestTemplate
提交表单
上传文件
设置超时
设置拦截器
Spring Boot
自动配置类为
RestTemplateAutoConfiguration
配置RestTemplate