我有一个MySql记录表,其中包含一个称为ConsignmentCode的列。我想发送一个HTTP请求,以通过ConsignmentCode搜索该表。

当我使用邮递员向http://localhost:8081/records/consignmentCode中的{consignmentCode: "123456789"}发出POST请求时,出现404 Not Found错误。有人知道为什么吗?

控制器类:

  /**
     * 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();
    }


道课:

 /**
     * 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

10-06 10:19