RestTemplateClient.java
2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.topdraw.resttemplate;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Objects;
@Slf4j
@Component
public class RestTemplateClient {
private static RestTemplate restTemplate;
private static String BASE_URL;
@Autowired
private Environment environment;
@PostConstruct
private void init() {
BASE_URL = environment.getProperty("api.uc-service");
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
// 设置连接超时
factory.setConnectTimeout(5000);
// 设置读取超时
factory.setReadTimeout(3000);
restTemplate = new RestTemplate(factory);
}
public static String netImage(String imageUrl) {
Image image = new Image(imageUrl);
String entityBody = "";
String url = BASE_URL + "/ucs/common/upload/netImage";
log.info("request uc : url is " + url + ", memberId is " + com.alibaba.fastjson.JSONObject.toJSONString(image));
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, image, String.class);
if (responseEntity.getStatusCode().is2xxSuccessful()) {
entityBody = responseEntity.getBody();
JSONObject parseObject = JSON.parseObject(entityBody);
List<Object> resultSet = (List)parseObject.get("resultSet");
Object o = resultSet.get(0);
return Objects.nonNull(o)?o.toString():"";
}
return entityBody;
}
@Data
static class Image {
private String url;
public Image(String imageUrl){
this.url = imageUrl;
}
}
}