【问题标题】:Simple POST request using Spring Boot and AWS使用 Spring Boot 和 AWS 的简单 POST 请求
【发布时间】:2016-12-20 05:11:39
【问题描述】:

我想制作一个简单的应用程序,将用户数据(名称和密码)存储到数据库中。为此,我创建了 EC2 和 RDS 实例。我在与 RDS 对话的 EC2 上运行 Spring Boot 应用程序。我的用户类如下所示:

@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;
    }
}

我希望能够在 EC2 localhost 上发出 GET 和 POST 请求,所以我添加了必要的存储库类:

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

现在,我应该可以像这样在我的 SQL 数据库中添加一个条目:

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

但是,我收到以下错误消息:

{"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"}

如果我删除“id”json 字段(因为它应该自动递增),我会收到相同的消息。

我有一个 AppConfig 类,其中包含我的 RDS 实例和标准主类的配置详细信息:

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

我还附加了 EC2 实例与 RDS 实例“对话”所需的安全组。有什么想法为什么我不能做这个简单的 POST 吗?感谢任何帮助。

【问题讨论】:

    标签: java spring api amazon-web-services amazon-ec2


    【解决方案1】:

    错误信息很奇怪,我期待类似

    Unexpected character
    

    问题肯定是在您的 curl 中,在您的消息中,您在用户名之前缺少双引号。正确的是:

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

    我猜你是在你的 ec2 实例中执行 curl 命令,因为你使用的是 localhost:8080。您确定可以访问正确的数据库吗?您的项目是否在本地环境中运行(可能使用 h2database)?

    还有一件事:作为第一步,您应该检查(并在发布时)堆栈跟踪,REST 响应中的消息是不够的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-29
      • 2017-08-10
      • 1970-01-01
      • 2018-07-18
      • 2023-03-23
      • 2021-03-06
      • 2015-11-06
      • 2017-09-21
      相关资源
      最近更新 更多