Closed Atreyu79 closed 4 years ago
Grabbed a drink and took another 20 minutes. Ended up figuring it out. See changes below.
@PostMapping("/v1/registerCustomer")
public String createRegisteredCustomer(@ModelAttribute RegisteredCustomerResource registeredCustomerResource) throws DocumentSerializationException {
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "application/vnd.api+json");
JSONAPIDocument<RegisteredCustomerResource> jsonApiDocumentRegisteredCustomer = new JSONAPIDocument<RegisteredCustomerResource>(
registeredCustomerResource);
HttpEntity<byte[]> httpEntity = new HttpEntity<byte[]>(
converter.writeDocument(jsonApiDocumentRegisteredCustomer), headers);
ResponseEntity<byte[]> response = restTemplate.exchange("http://localhost:8090/v1/registeredCustomers",
HttpMethod.POST, httpEntity, byte[].class);
JSONAPIDocument<RegisteredCustomerResource> postedRegisteredCustomerResource = converter
.readDocument(response.getBody(), RegisteredCustomerResource.class);
System.out.println(postedRegisteredCustomerResource.get().getId());
return "registration";
}
Hello @Atreyu79, glad you were able to figure it out.
~Regards
Good evening!
Might it be possible to provide an example of using jsonapi-converter for a POST using RestTemplate? Here's the code I currently have:
I'm using CRNK for the Java server-side implementation of JSON:API. The above code results in:
io.crnk.core.exception.RequestBodyException: Request body doesn't meet the requirements (No data field in the body.), POST method, resource name registeredCustomers
Here's a screenshot of my HttpEntity instance: