我有一个名为ConsignmentCode的MySql记录表。我想发送一个HTTP请求来通过ConsignmentCode搜索表。
当我使用Postman向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;
}
答案 0 :(得分:1)
使用:
@RequestBody String consignmentCode
而不是:
@RequestBody String params
答案 1 :(得分:0)
您不必期望接收String然后解析为JSon。它是Spring MVC中的内置行为。
只需导入jackson-dependencies。如果您使用Maven,请添加您的pom.xml依赖项:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.9</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.9</version>
</dependency>
然后以这种方式更改控制器:
@Controller
public class RestController {
@ResponseBody
@RequestMapping(
value = "/consignmentCode",
method = RequestMethod.POST,
consumes= MediaType.APPLICATION_JSON_VALUE)
public List<Record> SearchRecordsByConsignmentCode(@RequestBody ConsignmentCode code) {
System.out.print("this SearchRecordsByConsignmentCode respond happened");
List<Record> results=RecordDao.SearchRecordsByConsignmentCode(code.getConsignmentCode());
return results;
}
您需要创建一个封装消息正文的类:
import java.io.Serializable;
public class ConsignmentCode implements Serializable {
private String consignmentCode;
public String getConsignmentCode() {
return consignmentCode;
}
public void setConsignmentCode(String consignmentCode) {
this.consignmentCode = consignmentCode;
}
}
然后,启动它并尝试向您的控制器发送此消息(请记住将Accept / Content-Type标头设置为application/json
)。属性名称也必须在分号之间,这样:
{"consignmentCode":"123456789"}
您将以json格式获得正确的结果