我正在尝试通过 curl 命令将 JSON 字符串传递给 POST 方法。但是,我收到 HTTP 1.1 404 Not Found 错误。我正在尝试创建一个简单的 Web 服务,它将 JSON 字符串传递给它以填充 mysql 数据库。
这是我的 DAO 类,用于执行GETS 和POSTS。
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> [...]
请让我知道我哪里出错了。