mysql - 使用Spring框架发送POST请求

标签 mysql spring rest spring-mvc

我有一个 MySql 记录表,其中有一列名为 ConsignmentCode。我想发送一个 HTTP 请求来通过 ConsignmentCode 搜索表。

当我使用 Postman 向 http://localhost:8081/records/consignmentCode 发送 POST 请求(正文中包含 {consignmentCode: "123456789"})时,出现 404 Not Found 错误。有谁知道为什么吗?

Controller 类:

  /**
     * POST request to search by ConsignmentCode
     *
     * /records/consignmentCode
     *
     * Input ex: {consignmentCode: "123456789"}
     * @param params
     * @return
     */

    @CrossOrigin
    @ResponseBody
    @RequestMapping(
            value = "/consignmentCode",
            method = RequestMethod.POST,
            consumes= MediaType.APPLICATION_JSON_VALUE)
    public java.lang.String SearchRecordsByConsignmentCode(@RequestBody String params) {
        System.out.print("this SearchRecordsByConsignmentCode respond happened");
        JSONObject obj = new JSONObject();
        JSONObject jsonObj = new JSONObject(params);
        String likeConsignment= jsonObj.getString("consignmentCode");

        List<record> results=RecordDao.SearchRecordsByConsignmentCode(likeConsignment);
        obj.put("results", results);
        return obj.toString();
    }

Dao 类:

 /**
     * Search by ConsignmentCode.
     *
     * @param consignmentCode
     * @return
     */

    public List<record> SearchRecordsByConsignmentCode(String consignmentCode) {
        final String sql = "SELECT * FROM records WHERE ConsignmentCode = ?";
        List<record> Record = jdbcTemplate.query(sql, new RowMapper<record>() {
            public record mapRow(ResultSet resultSet, int Id) throws SQLException {
                record records = new record();
                records.setId(resultSet.getInt("Id"));
                records.setNumber(resultSet.getString("Number"));
                records.setTitle(resultSet.getString("Title"));
                records.setScheduleId(resultSet.getInt("ScheduleId"));
                records.setTypeId(resultSet.getInt("TypeId"));
                records.setConsignmentCode(resultSet.getString("ConsignmentCode"));
                records.setStateId(resultSet.getInt("StateId"));
                records.setContainerId(resultSet.getInt("ContainerId"));
                records.setLocationId(resultSet.getInt("LocationId"));
                records.setCreatedAt(resultSet.getDate("CreatedAt"));
                records.setUpdatedAt(resultSet.getDate("UpdatedAt"));
                records.setClosedAt(resultSet.getDate("ClosedAt"));
                System.out.print(records);
                return records;
            }
        }, consignmentCode);
        return Record;
    }

最佳答案

使用:

@RequestBody String consignmentCode

而不是:

@RequestBody String params

关于mysql - 使用Spring框架发送POST请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46898966/

相关文章:

rest - 有没有一种标准方法可以在 HTTP header 字段中定义 CURIE?

mysql - 使用 PHP 和 Mysql 进行库存管理

php - Django + XAMPP on Mac (2002, "Can' t 通过套接字 '/tmp/mysql.sock' (2)"连接到本地 MySQL 服务器")

java - 用户登录和 session 劫持后的 Spring Security cookie

security - 如何从 RESTful 服务发送安全 token ?

python - 如何在 Django 中生成 303 Http 响应?

mysql - 比较日期时间不起作用

php - 表单用户名和数据库用户名不一致,即使它们相同

java - 使用什么策略来确定 JSON 或 XML?

java - "container"和 "context"是一种设计模式吗?