jasminb / jsonapi-converter

JSONAPI-Converter is a Java/Android library that provides support for working with JSONAPI spec
Apache License 2.0
273 stars 81 forks source link

POST Example with RestTemplate #237

Closed Atreyu79 closed 4 years ago

Atreyu79 commented 4 years ago

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:

    @PostMapping("/v1/registerCustomer")
    public String createRegisteredCustomer(@ModelAttribute RegisteredCustomerResource registeredCustomerResource) {

        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type", "application/vnd.api+json");

        JSONAPIDocument<RegisteredCustomerResource> jsonApiDocumentRegisteredCustomer = new JSONAPIDocument<RegisteredCustomerResource>(
                registeredCustomerResource);

        HttpEntity<JSONAPIDocument<RegisteredCustomerResource>> httpEntity = new HttpEntity<JSONAPIDocument<RegisteredCustomerResource>>(
                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";
    }

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:

HttpEntity

Atreyu79 commented 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";
    }
jasminb commented 4 years ago

Hello @Atreyu79, glad you were able to figure it out.

~Regards