gpt4 book ai didi

java - 使用 curl 发布 POST 时获取 HTTP 404 Not Found

转载 作者:行者123 更新时间:2023-11-29 03:39:02 25 4
gpt4 key购买 nike

我正在尝试通过 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 命令时,它工作正常并且返回 person 表中的所有值。但是,如果我发出 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> [...]

请让我知道哪里出错了。

最佳答案

我假设您发出的命令中一定缺少 -H 'Content-Type:application/json'

关于java - 使用 curl 发布 POST 时获取 HTTP 404 Not Found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14048394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com