RestConnector POST method with an entity/instance (headers and/or body) to write to the request and a different response type instance

What product/components do you use and which version/fix level are you on?

C8Y MS SDK 1009.0.12

Is your question related to the free trial, or to a production (customer) instance?

What are you trying to achieve? Please describe it in detail.

Basically, trying to replace Spring Framework’s RestTemplate implementation with RestConnector managed by the microservice SDK. (Cumulocity IoT SDK/Java client supports /group/* API methods? - #4 by Harald_Meyer)

We are trying to replicate below piece of code:

@Autowired
private PlatformRestTemplate restTemplate; // PlatformRestTemplate extends RestTemplate
private ResponseEntity<?> validateRuleInApama(RuleValidationRequest ruleValidationRequest)
 {
			HttpEntity<RuleValidationRequest> requestEntity = new HttpEntity<>(ruleValidationRequest);
			ResponseEntity<RuleValidationResponse> responseEntity = restTemplate.exchange(apamaUrl + "/validator/rule", HttpMethod.POST, requestEntity, RuleValidationResponse.class);
			return responseEntity;
	}

The post methods from RestConnector instance accept a common request and response instance types cumulocity-clients-java/RestConnector.java at develop · SoftwareAG/cumulocity-clients-java · GitHub but not flexible as above.

Looking for corresponding classes/methods in the java client! Maybe they are added in a more recent version of the client?


Best,
Rahul

Do you get any error messages? Please provide a full error message screenshot and log file.

Have you installed all the latest fixes for the products and systems you are using?

Hi, that is not doable with RestConnector unless you can make your request and response classes implement ResourceRepresentation.

Alternatively you can also use the standard HTTP client of Java (if the reason for moving from RestTemplate is its deprecation):

ObjectMapper mapper = JsonMapper.builder()
		    .addModule(new JavaTimeModule())
		    .build();


HttpRequest postRequest = HttpRequest.newBuilder()
		.uri(URI.create((host + "/myservice")))
		.headers("Authorization", subscriptionsService.getCredentials(subscriptionsService.getTenant()).get().toCumulocityCredentials().getAuthenticationString(),
				"Accept", "application/json", 
				"Content-Type", "application/json")
		.POST(HttpRequest.BodyPublishers.ofString(mapper.writeValueAsString(request)))
		.build();

HttpResponse<String> response = httpClient.send(postRequest, HttpResponse.BodyHandlers.ofString());
mapper.readValue(response.body(), MyResponseClass.class)

request is an instance of the payload you want to send.

Thanks a lot Harald! Your code snipped helped a lot!

Just one remark:

The problem/limitation with the RestConnector is not the requirement to implement ResourceRepresentation but that the request object and the response object must be both of the exact same type!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.