Cet article est un rappel du code source qui atteint l'API REST de Salesforce à partir de Java. Comme de nombreuses parties sont omises, telles que les fichiers de propriétés, cela ne fonctionne pas même si vous l'écrivez tel quel.
Pour le moment, j'aimerais connaître les trois points suivants.
demande
JSON
{
"request" : [
[
"id" : "000",
"name" : "sample0"
]
[
"id" : "001",
"name" : "sample1"
]
]
}
réponse
JSON
{
"results" : [
[
"id" : "000",
"name" : "sample0",
"status" : "200"
]
[
"id" : "001",
"name" : "sample1",
"status" : "200"
]
]
}
ConnectApiLogic.java
public class ConnectApiLogic {
private static final String LOGIN_URL = PropertyUtil.getProperty("SF_LOGIN_URL");
private static final String GRANT_TYPE = PropertyUtil.getProperty("SF_GRANT_TYPE");
private static final String CLIENT_ID = PropertyUtil.getProperty("SF_CLIENT_ID");
private static final String CLIENT_SECRET = PropertyUtil.getProperty("SF_CLIENT_SECRET");
private static final String USERNAME = PropertyUtil.getProperty("SF_USERNAME");
private static final String PASSWORD_AND_SECURITY_TOKEN = PropertyUtil.getProperty("SF_PASSWORD_AND_SECURITY_TOKEN");
private static String url = PropertyUtil.getProperty("SF_RESTPASS") + PropertyUtil.getProperty("SF_UPDATE_USERMST");
RestTemplate restTemplate = new RestTemplate();
/**Une méthode pour donner une liste et obtenir le résultat de la requête*/
public ResponseDTO getResults(List<RequestChildDTO> requestList) {
//Obtenir un jeton d'authentification
ResponseEntity<ApiTokenResponseDto> apiTokenResponse = doPostAccess(CLIENT_ID, CLIENT_SECRET);
String authToken = apiTokenResponse.getBody().getAccessToken();
if (authToken.isBlank()) {
System.exit(1);
}
RequestParentDTO requestDTO = new RequestUserMstDTO();
requestDTO.request = requestList;
//Création d'en-tête
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
headers.add("Authorization","Bearer "+authToken);
headers.add("Content-Type", "application/json");
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
HttpEntity<RequestParentDTO> request = new HttpEntity<RequestParentDTO>(requestDTO, headers);
return restTemplate.postForObject(url, request, ResponseParentDTO.class);
}
/**Méthode d'acquisition de jetons d'accès*/
private ResponseEntity<ApiTokenResponseDto> doPostAccess(String cliendId, String clientSecret) {
MultiValueMap<String, String> request = new LinkedMultiValueMap<String, String>();
request.add("grant_type", GRANT_TYPE);
request.add("client_id", cliendId);
request.add("client_secret", clientSecret);
request.add("username", USERNAME);
request.add("password", PASSWORD_AND_SECURITY_TOKEN);
RequestEntity requestEntity = null;
ResponseEntity<ApiTokenResponseDto> response = null;
try {
requestEntity = RequestEntity.post(new URI(LOGIN_URL)).contentType(MediaType.APPLICATION_FORM_URLENCODED).body(request);
response = restTemplate.exchange(requestEntity, ApiTokenResponseDto.class);
} catch (URISyntaxException e) {
e.printStackTrace();
return response;
} catch (HttpClientErrorException e) {
e.printStackTrace();
return response;
}
return response;
}
}
RequestParentDTO.java
public class RequestParentDTO {
public List<RequestChildDTO> request;
public RequestUserMstDTO(){
request = new ArrayList<>();
}
}
RequestChildDTO.java
public class RequestChildDTO {
public String id;
public String name;
}
ResponseParentDTO.java
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"results"
})
public class ResponseParentDTO {
@JsonProperty("results")
public List<ResponseChild> results = null;
public ResponseDTO(){}
}
ResponseChild.java
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"id",
"name",
"status",
})
public class ResponseChild {
@JsonProperty("id")
public String id;
@JsonProperty("name")
public String name;
@JsonProperty("status")
public String status;
public Result(){}
}
PostApi.cls
@RestResource(urlMapping='/sample/*')
global without sharing class PostApi {
@HttpPost
global static DT_ResponseParentDto doPost() {
//Acquisition de paramètres
RestRequest req = RestContext.request;
List<InnerRequest> innnerRequestList = setRequest(req);
//Réponse de retour
return setResponse(innnerRequestList);
}
/**Demander la méthode d'acquisition des paramètres*/
private static List<InnerRequest> setRequest(RestRequest req) {
Map<String, Object> requestParams = (Map<String, Object>) JSON.deserializeUntyped(req.requestBody.ToString());
List<Object> requestList = (List<Object>) requestParams.get('request');
List<InnerRequest> innerRequestList = new List<InnerRequest>();
for (Object obj : requestList) {
Map<String, Object> params = (Map<String, Object>) obj;
InnerRequest inner = new InnerRequest();
inner.id = (String) params.get('id');
inner.name = (String) params.get('name');
innerRequestList.add(inner);
}
return innerRequestList;
}
/**Méthode de création de réponse*/
private static DT_ResponseParentDto setResponse(List<InnerRequest> innnerRequestList) {
DT_ResponseParentDto response = new DT_ResponseParentDto();
//Définissez le code d'état dans la liste de réussite
if (innnerRequestList.size() > 0) {
for (InnerRequest inner : innnerRequestList) {
DT_ResponseChildDto child = new DT_ResponseChildDto();
child.id = inner.id;
child.name = inner.name;
child.status = "200";
response.results.add(child);
}
}
return response;
}
/**Classe interne pour le stockage des demandes*/
public class InnerRequest {
public String id;
public String name;
}
}
DT_ResponseParentDto.cls
public class DT_ResponseParentDto{
public List<DT_ResponseChildDto> results;
public DT_ResponseParentDto() {
results = new List<DT_ResponseChildDto>();
}
}
DT_ResponseChildDto.cls
public class DT_ResponseChildDto{
public String id;
public String name;
public String status;
}
Recommended Posts