michaellavelle / spring-data-dynamodb

Simplifies the development of creating an Amazon DynamoDB-based Java data access layer
https://derjust.github.io/spring-data-dynamodb/
Apache License 2.0
169 stars 284 forks source link

How to post to a URL with hash + range key #32

Closed jAddict7 closed 10 years ago

jAddict7 commented 10 years ago

Domain Class:

    @DynamoDBTable(tableName = "test")
    public class Test implements Serializable{

private static final long serialVersionUID = 1L;

@Id private TestId testId;
private String description;
private String testing;

@DynamoDBHashKey(attributeName="id")
public String getId() {
    return testId != null ? testId.getId() : null;
}

public void setId(String id) {
    if(testId == null){
        testId = new TestId();
    }
    this.setId(id);
}

@DynamoDBRangeKey(attributeName="name")
public String getName() {
    return testId != null ? testId.getName() : null;
}

public void setName(String name) {
    if(testId == null){
        testId = new TestId();
    }
    this.setName(name);
}

@DynamoDBAttribute(attributeName="description")
public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

@DynamoDBAttribute(attributeName="testing")
public String getTesting() {
    return testing;
}

public void setTesting(String testing) {
    this.testing = testing;
}

public TestId getTestId() {
    return testId;
}

public void setTestId(TestId testId) {
    this.testId = testId;
}
    }

Supporter Class:

    public class TestId implements Serializable{

private String id;
private String name;

@DynamoDBHashKey
public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

@DynamoDBRangeKey
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
    }

I think I have created Domain class correctly but What is the correct procedure to Post data into it. I have tried, URL: http://localhost:8080/tests Request Body: {"testId": {"id": "gowtham", "name": "z"}, "description": "Awesome Guy", "testing": "x"} and {"id": "gowtham", "name": "z", "description": "Awesome Guy", "testing": "x"}

But both failed and throws an exception as, { cause: { cause: { cause: null message: null }- message: "N/A (through reference chain: pkg.Test["id"])" }- message: "Could not read JSON: N/A (through reference chain: pkg.Test["id"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: N/A (through reference chain: pkg["id"])" }

But id object is present. What is the correct way to pass argument in POST method?

michaellavelle commented 10 years ago

You've raised a couple of issues you are having with the combination of spring-data-rest and spring-data-dynamodb that are used in the demo. Thank you for raising these - I will see if I can find some time at some point to resolve them.

It seems that there have been some changes to spring-data-rest since I created the demo and some things no longer work as they did - for example the ids are not displayed correctly in the JSON response and you are having issues posting.

I feel I need to explain that my work has been on the spring-data-dynamodb project, and I was just using spring-data-rest as an easy way to wire up spring-data-dynamob and demonstrate it is working.

Issues that relate to spring-data-rest ( urls to post to, id conversion etc) are not something I'm going to be able to help you with immediately unfortunately. I haven't yet had the chance to read up about changes that have been made to spring-data-rest since I created the demo.

It seems that the functionality of spring-data-dynamodb is working for you ( with the possible exception of the @NotNull validation that you have raised separately, which I hope to look into soon) - but this issue with JSON binding is spring-data-rest issue

Perhaps it may help you to resolve these issues if you direct questions about spring-data-rest to the community leads for that project, as these are not directly related to spring-data-dynamodb and I'm not really in a position to resolve these at the moment.

jAddict7 commented 10 years ago

Yeah I understand, Sort issue comes under Spring-Data-DynamoDB I think so, if possible have a look at it. I have done validation using basic validators as far now and working fine. Sorting makes me a big deal till now.

jAddict7 commented 10 years ago

Here in Test.java, * this.setId(id);* and this.setName(name); should be replaced with * this.testId.setId(id);* and this.testId.setName(name); respectively.