Dieser Artikel erinnert an den Quellcode, der die REST-API von Salesforce von Java aus trifft. Da viele Teile weggelassen werden, z. B. Eigenschaftendateien, funktioniert dies nicht, selbst wenn Sie es so schreiben, wie es ist.
Vorerst möchte ich die folgenden drei Punkte wissen.
Anfrage
JSON
{
"request" : [
[
"id" : "000",
"name" : "sample0"
]
[
"id" : "001",
"name" : "sample1"
]
]
}
Antwort
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();
/**Eine Methode, um eine Liste zu erstellen und das Anforderungsergebnis abzurufen*/
public ResponseDTO getResults(List<RequestChildDTO> requestList) {
//Holen Sie sich das Authentifizierungstoken
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;
//Header-Erstellung
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);
}
/**Zugriffstoken-Erfassungsmethode*/
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() {
//Parametererfassung
RestRequest req = RestContext.request;
List<InnerRequest> innnerRequestList = setRequest(req);
//Antwort zurückgeben
return setResponse(innnerRequestList);
}
/**Parametererfassungsmethode anfordern*/
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;
}
/**Methode zur Erstellung von Antworten*/
private static DT_ResponseParentDto setResponse(List<InnerRequest> innnerRequestList) {
DT_ResponseParentDto response = new DT_ResponseParentDto();
//Stellen Sie den Statuscode in der Erfolgsliste ein
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;
}
/**Innere Klasse zur Speicherung von Anforderungen*/
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