Spring Boot app with OneSpan Sign RESTful API
Monday, April 29, 2019 at 11:38amHi,
I'm looking to integrate the OneSpan Sign API with our web application using Spring Boot. Is there any sample code available to help me get started?
Thanks,
OneSpan Introduces DigipassONE, Bringing a Unified Platform Approach to Authentication Modernization
A new authentication platform helps financial institutions support diverse customer authentication preferences while modernizing at their own pace Learn More
Reply to: Spring Boot app with OneSpan Sign RESTful API
Monday, April 29, 2019 at 11:55amReply to: Spring Boot app with OneSpan Sign RESTful API
Monday, April 29, 2019 at 12:00pmReply to: Spring Boot app with OneSpan Sign RESTful API
Tuesday, May 21, 2019 at 08:34pmReply to: Spring Boot app with OneSpan Sign RESTful API
Wednesday, May 22, 2019 at 04:47amHey Sumanta, Yes, for sure OneSpan Sign API works with RestTemplate. Like I replied in this thread, could do an extra error handling for your Spring RestTemplate something like below:
try { ResponseEntity result = restTemplate.exchange(url, HttpMethod.GET, entity, Package.class, params); System.out.println(result); } catch (HttpStatusCodeException exception) { int statusCode = exception.getStatusCode().value(); String errorMessage = exception.getResponseBodyAsString(); System.out.println(statusCode + " : " + errorMessage); }Which would receive the error stream from OneSpan Sign which contains the exact error message. Then we can troubleshoot the cause of the error. Hope this could help! Duo
Reply to: Spring Boot app with OneSpan Sign RESTful API
Wednesday, May 22, 2019 at 08:16pmHttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); httpHeaders.add(Constants.HeaderKeys.AUTHORIZATION, Constants.AUTH_TYPE + " " + propertyLoader.getAppProperties().getProperty(Constants.PropertyKeys.ONESPAN_API_KEY)); httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); httpHeaders.add("Accept", "application/json; esl-api-version=11.0"); MultiValueMap multipartFileMap = new LinkedMultiValueMap>();
MultiValueMap payloadMap = new LinkedMultiValueMap>();
MultiValueMap requestDataMap = new LinkedMultiValueMap>();
Iterator attachmentIterator = apttusAttachmentDocList.iterator();
while (attachmentIterator.hasNext()) {
AttachedDocumentFileBean attachedDocumentFile = (AttachedDocumentFileBean) attachmentIterator.next();
InputStream inputStream = apttusAPIBean.getDocumentInfoForDocumentId(attachedDocumentFile.getId());
logger.info("Input Stream Apttus API :: " + inputStream);
if (inputStream == null) {
logger.error("File content not found...");
throw new ApttusAPIExecutionException("Failed to get the file content");
}
//ByteArrayResource byteArrayResource = new ByteArrayResource(IOUtils.toByteArray(inputStream));
ContentDisposition contentDisposition = ContentDisposition.builder("form-data")
.name(Constants.FILE_KEY)
.filename(attachedDocumentFile.getName())
.build();
logger.info("Content Disposition :: " + contentDisposition.toString());
multipartFileMap.add(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString());
multipartFileMap.add(HttpHeaders.CONTENT_TYPE, "application/pdf");
HttpEntity fileContent = new HttpEntity>(IOUtils.toByteArray(inputStream), multipartFileMap);
logger.info("Http Entity File Content" + fileContent);
requestDataMap.add(Constants.FILE_KEY, fileContent);
fileSet.add(attachedDocumentFile.getName());
}
ArtifactBean artefact = payloadBean.prepareArtefactWithRoles(packagePayload, fileSet);
ObjectMapper objectMapper = new ObjectMapper();
String payload = objectMapper.writeValueAsString(artefact);
logger.info("Payload Json For Artefact :: " + payload);
ContentDisposition payloadContentDisposition = ContentDisposition.builder("form-data")
.name(Constants.PAYLOAD_KEY)
.build();
payloadMap.add(HttpHeaders.CONTENT_DISPOSITION, payloadContentDisposition.toString());
payloadMap.add(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8");
HttpEntity payloadContent = new HttpEntity>(payload, payloadMap);
requestDataMap.add(Constants.PAYLOAD_KEY, payloadContent);
logger.info("Http Entity Payload Content" + payloadContent);
HttpEntity> requestEntity = new HttpEntity>(requestDataMap, httpHeaders);
logger.info("Http Entity Request Content" + requestEntity);
String PACKAGE_CREATION_URL= Constants.OneSpanAPIEndPoints.POST_PACKAGE_ESIGNATURE;
UriComponentsBuilder packageCreateApiUrlBuilder = UriComponentsBuilder.fromHttpUrl(PACKAGE_CREATION_URL);
PACKAGE_CREATION_URL = packageCreateApiUrlBuilder.build().encode().toUri().toString();
logger.info("Start to call onespan package creation api...");
logger.info("Package Creation URL :: " + PACKAGE_CREATION_URL);
ResponseEntity packageCreateResponse = restTemplate.exchange(PACKAGE_CREATION_URL,
HttpMethod.POST,
requestEntity,
OneSpanPackageBean.class);
Thanks SumantaReply to: Spring Boot app with OneSpan Sign RESTful API
Thursday, May 23, 2019 at 06:23amtry { ResponseEntity response = restTemplate.exchange(PACKAGE_CREATION_URL,
HttpMethod.POST, requestEntity, OneSpanPackageBean.class);
return response.getBody();
} catch (HttpStatusCodeException exception) {
return exception.getResponseBodyAsString();
}
And for a working full code, here's a function used to create a transaction(with hardcoded metadata):@PostMapping("/upload") public String uploadFile(){ MultiValueMap parts = new LinkedMultiValueMap();
parts.add("file", "123");
parts.add("payload",
"{\"roles\":[{\"id\":\"Role1\",\"signers\":[{\"email\":\"[email protected]\",\"firstName\":\"1.firstname\",\"lastName\":\"1.lastname\",\"company\":\"OneSpan Sign\"}]},{\"id\":\"Role2\",\"signers\":[{\"email\":\"[email protected]\",\"firstName\":\"2.firstname\",\"lastName\":\"2.lastname\",\"company\":\"OneSpan Sign\"}]}],\"documents\":[{\"approvals\":[{\"role\":\"Role1\",\"fields\":[{\"page\":0,\"top\":100,\"subtype\":\"FULLNAME\",\"height\":50,\"left\":100,\"width\":200,\"type\":\"SIGNATURE\"}]},{\"role\":\"Role2\",\"fields\":[{\"page\":0,\"top\":300,\"subtype\":\"FULLNAME\",\"height\":50,\"left\":100,\"width\":200,\"type\":\"SIGNATURE\"}]}],\"name\":\"Test Document\"}],\"name\":\"Example Package\",\"type\":\"PACKAGE\",\"language\":\"en\",\"emailMessage\":\"\",\"description\":\"New Package\",\"autocomplete\":true,\"status\":\"SENT\"}");
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.add("Accept", "application/json; esl-api-version=11.0");
headers.add("Authorization", "Basic your_api_key");
HttpEntity> requestEntity = new HttpEntity>(parts,
headers);
try {
ResponseEntity response = restTemplate.exchange("https://sandbox.esignlive.com/api/packages",
HttpMethod.POST, requestEntity, String.class);
return response.getBody();
} catch (HttpStatusCodeException exception) {
return exception.getResponseBodyAsString();
}
}
Simply change the API_KEY and PACKAGE_CREATION_URL to yours and you'll see the output. Hope this could help! Duo