ClientCustomerServiceHttpClient.java
1.95 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
package com.infoloop.tianting.service.client;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.infoloop.tianting.config.BusinessConfig;
import com.infoloop.tianting.model.dto.ClientCustomerDbDTO.CurrentClientCustomerDto;
import com.infoloop.tianting.model.dto.ClientCustomerDbDTO.CurrentClientCustomerListDto;
import lombok.RequiredArgsConstructor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ClientCustomerServiceHttpClient {
private final BusinessConfig businessConfig;
private final OkHttpClient client = new OkHttpClient();
public List<CurrentClientCustomerDto> getCurrentClientCustomer(String token, boolean isHis) {
ObjectMapper objectMapper = new ObjectMapper();
try {
String url;
if (isHis) {
url = businessConfig.getHisCustomerQueryUrl();
} else {
url = businessConfig.getClientCustomerQueryUrl();
}
MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(mediaType, "{}");
Request request = new Request.Builder()
.url(url)
.post(body)
.addHeader("Authorization", token)
.build();
Response response = client.newCall(request).execute();
String jsonData = response.body().string();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return objectMapper.readValue(jsonData, CurrentClientCustomerListDto.class).getRows();
} catch (Exception e) {
System.out.println("error"+e.getMessage());
throw new IllegalArgumentException("获取当日在住客户失败");
}
}
}