简体   繁体   中英

Simple POST request using Spring Boot and AWS

I want to make a simple application that stores user data (name and password) into a database. To do this, I created EC2 and RDS instances. I run the Spring Boot application on EC2 which talks to RDS. My User class looks like this:

@Entity
@Table(name="users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id")
    private long id;
    @Column(name="name")
    private String name;
    @Column(name="password")
    private String password;

    public long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

I want to be able to make GET and POST requests on EC2 localhost, so I added the necessary repository class:

@RepositoryRestResource(collectionResourceRel = "usertest", path = "usertest")
public interface UserRepository extends PagingAndSortingRepository<User, Long> {
}

Now, I should be able to add an entry into my SQL database like so:

curl -H "Content-Type: application/json" -X POST -d '{"id": "102", username":"john", "password":"xyz"}' http://localhost:8080/usertest

However, I get the following error message:

{"timestamp":1482172780645,"status":500,"error":"Internal Server Error","exception":"org.springframework.dao.InvalidDataAccessResourceUsageException","message":"could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet","path":"/usertest"}

I get the same message if I remove the "id" json field (since it should auto-increment).

I have an AppConfig class with configuration details for my RDS instance and the standard main class:

@SpringBootApplication
@EnableJpaRepositories
public class Application {
        public static void main(String[] args) throws Exception {
            SpringApplication.run(Application.class, args);
        }
}

I have also attached the security group needed for the EC2 instance to "talk" to the RDS instance. Any ideas why I can't make this simple POST? Appreciate any help.

The error message is weird, I was expecting something like

Unexpected character

On of the problem is for sure in your curl, in your message you are missing the double quotes before username. The correct one is the following:

curl -H "Content-Type: application/json" -X POST -d '{"id": "102", "username":"john", "password":"xyz"}' http://localhost:8080/usertest

I guess that you are executing the curl command in your ec2 instance, because you are using localhost:8080. Are you sure that you can reach the right database? Is your project working on your local environment (maybe using h2database)?

One more thing: you should check (and in case post) the stacktrace as first step, a message in a REST response is not enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM