我試圖通過curl命令將JSON字符串傳遞給POST方法。但是,我收到HTTP 1.1 404 Not Found錯誤。我正在嘗試創建一個簡單的Web服務,它將JSON字符串傳遞給它以填充MySQL數據庫。使用curl發出POST時未獲得HTTP 404
這是我用來做GETS和POSTS的DAO類。
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class PersonDAO {
public List<Person> findAll() {
List<PErson> list = new ArrayList<Person>();
Connection c = null;
String sql = "SELECT * FROM person ORDER BY firstName";
try {
c = ConnectionHelper.getConnection();
Statement s = c.createStatement();
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
list.add(processRow(rs));
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
ConnectionHelper.close(c);
}
return list;
public Person create(Person person) {
Connection c = null;
PreparedStatement ps = null;
try {
c = ConnectionHelper.getConnection();
ps = c.prepareStatement("INSERT INTO Person (firstName,lastName) VALUES (?, ?)",
new String[] { "ID" });
ps.setString(1, person.getfirstName());
ps.setString(2,person.getlastName());
ps.executeUpdate();
ResultSet rs = ps.getGeneratedKeys();
rs.next();
int id = rs.getInt(1);
person.setId(id);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
ConnectionHelper.close(c);
}
return person;
}
protected Person processRow(ResultSet rs) throws SQLException {
Person person = new Person();
person.setfirstName(rs.getString("firstname"));
person.setlastName(rs.getString("lastname"));
return person;
}
我的POJO
@XmlRootElement
public class Person{
private Integer id;
private String firstName;
private String lastName;
//GETTERS AND SETTERS
}
我的註釋
我一個人的資源類:
@Path("/people")
public class PersonResource{
PersonDAO dao = new PersonDAO();
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Person> findAll(){
System.out.println("findAll");
return dao.findAll();
}
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Person creare(Person person){
System.out.println("creating person");
return dao.create(person);
}
當我發出GET curl命令它工作正常,並返回該人的所有值表。但是,如果我發出POST curl命令,則會出現以下錯誤:
HTTP/1.1 404 Not Found
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Length: 1025
Date: Thu, 27 Dec 2012 01:33:41 GMT
<html><head><title>Apache Tomcat/7.0.32 - Error report</title><style> [...]
請讓我知道我要去哪裏出錯。
(IMO的DAO代碼在這一點上不相關,更有用的是你使用的精確的curl命令,明確的框架等) –
@Srinivas,謝謝你的信息..我的curl命令是這樣的:curl -i -X POST -H'Content-Type:application/json'-d'{「firstname」:「John」,「lastname」 :「Doe」}'http:// localhost:8080/persondb/rest/people – california6586
@ california6586和你使用的'GET'的curl命令是否正常工作? – Srinivas