From 6f494f46266e8709d6b61530aa48fe76adacf965 Mon Sep 17 00:00:00 2001 From: sourabh_sourabh Date: Wed, 5 Jan 2022 23:50:20 +0530 Subject: CPS-Core: Unable to parse JSON input with space for POST endpoint Issue-ID: CPS-831 Signed-off-by: sourabh_sourabh Change-Id: If2f5f7034f05763990001c9dd8ccd9d8dc0099cf --- .../onap/cps/rest/controller/DataRestController.java | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'cps-rest/src/main/java/org') diff --git a/cps-rest/src/main/java/org/onap/cps/rest/controller/DataRestController.java b/cps-rest/src/main/java/org/onap/cps/rest/controller/DataRestController.java index 73c2c27c91..a55b1ba7a1 100755 --- a/cps-rest/src/main/java/org/onap/cps/rest/controller/DataRestController.java +++ b/cps-rest/src/main/java/org/onap/cps/rest/controller/DataRestController.java @@ -22,15 +22,16 @@ package org.onap.cps.rest.controller; +import com.google.gson.Gson; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; import javax.validation.ValidationException; +import lombok.RequiredArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.onap.cps.api.CpsDataService; import org.onap.cps.rest.api.CpsDataApi; import org.onap.cps.spi.FetchDescendantsOption; import org.onap.cps.utils.DataMapUtils; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; @@ -38,23 +39,24 @@ import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("${rest.api.cps-base-path}") +@RequiredArgsConstructor public class DataRestController implements CpsDataApi { private static final String ROOT_XPATH = "/"; private static final String ISO_TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; private static final DateTimeFormatter ISO_TIMESTAMP_FORMATTER = DateTimeFormatter.ofPattern(ISO_TIMESTAMP_FORMAT); - @Autowired - private CpsDataService cpsDataService; + private final CpsDataService cpsDataService; + private final Gson gson; @Override public ResponseEntity createNode(final String dataspaceName, final String anchorName, final Object jsonData, final String parentNodeXpath, final String observedTimestamp) { if (isRootXpath(parentNodeXpath)) { - cpsDataService.saveData(dataspaceName, anchorName, jsonData.toString(), + cpsDataService.saveData(dataspaceName, anchorName, gson.toJson(jsonData), toOffsetDateTime(observedTimestamp)); } else { - cpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, jsonData.toString(), + cpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, gson.toJson(jsonData), toOffsetDateTime(observedTimestamp)); } return new ResponseEntity<>(HttpStatus.CREATED); @@ -71,7 +73,7 @@ public class DataRestController implements CpsDataApi { @Override public ResponseEntity addListElements(final String parentNodeXpath, final String dataspaceName, final String anchorName, final Object jsonData, final String observedTimestamp) { - cpsDataService.saveListElements(dataspaceName, anchorName, parentNodeXpath, jsonData.toString(), + cpsDataService.saveListElements(dataspaceName, anchorName, parentNodeXpath, gson.toJson(jsonData), toOffsetDateTime(observedTimestamp)); return new ResponseEntity<>(HttpStatus.CREATED); } @@ -89,7 +91,7 @@ public class DataRestController implements CpsDataApi { @Override public ResponseEntity updateNodeLeaves(final String dataspaceName, final String anchorName, final Object jsonData, final String parentNodeXpath, final String observedTimestamp) { - cpsDataService.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, jsonData.toString(), + cpsDataService.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, gson.toJson(jsonData), toOffsetDateTime(observedTimestamp)); return new ResponseEntity<>(HttpStatus.OK); } @@ -98,7 +100,7 @@ public class DataRestController implements CpsDataApi { public ResponseEntity replaceNode(final String dataspaceName, final String anchorName, final Object jsonData, final String parentNodeXpath, final String observedTimestamp) { cpsDataService - .replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, jsonData.toString(), + .replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, gson.toJson(jsonData), toOffsetDateTime(observedTimestamp)); return new ResponseEntity<>(HttpStatus.OK); } @@ -107,7 +109,7 @@ public class DataRestController implements CpsDataApi { public ResponseEntity replaceListContent(final String parentNodeXpath, final String dataspaceName, final String anchorName, final Object jsonData, final String observedTimestamp) { - cpsDataService.replaceListContent(dataspaceName, anchorName, parentNodeXpath, jsonData.toString(), + cpsDataService.replaceListContent(dataspaceName, anchorName, parentNodeXpath, gson.toJson(jsonData), toOffsetDateTime(observedTimestamp)); return new ResponseEntity<>(HttpStatus.OK); } -- cgit 1.2.3-korg