Java 使用curl发出POST时找不到获取HTTP 404

Java 使用curl发出POST时找不到获取HTTP 404,java,json,curl,jersey,http-post,Java,Json,Curl,Jersey,Http Post,我试图通过curl命令将JSON字符串传递给POST方法。但是,我收到一个HTTP 1.1 404 Not Found错误。我试图创建一个简单的web服务,将JSON字符串传递给它以填充mysql数据库 这是我的DAO类,用于执行GET和POST import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sq

我试图通过curl命令将JSON字符串传递给POST方法。但是,我收到一个HTTP 1.1 404 Not Found错误。我试图创建一个简单的web服务,将JSON字符串传递给它以填充mysql数据库

这是我的DAO类,用于执行GET和POST

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;
    }
我的批注的我的个人资源类:

@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);
}
@Path(“/people”)
公共类人员资源{
PersonDAO=新PersonDAO();
@得到
@产生(MediaType.APPLICATION_JSON)
公共列表findAll(){
System.out.println(“findAll”);
返回dao.findAll();
}
@职位
@产生(MediaType.APPLICATION_JSON)
@使用(MediaType.APPLICATION_JSON)
公众人物(个人){
System.out.println(“创建人”);
返回道。创建(人);
}
当我发出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> [...]
未找到HTTP/1.1 404 服务器:ApacheCoote/1.1 内容类型:text/html;字符集=utf-8 内容长度:1025 日期:2012年12月27日星期四格林尼治时间01:33:41 ApacheTomcat/7.0.32-错误报告[…]
请告诉我哪里出了问题。

在方法之前的帖子中,添加路径,尝试。

在方法之前的帖子中,添加路径,尝试。

我假设您发出的命令中一定缺少-H'内容类型:application/json'

我假设您在命令中一定缺少-H'内容类型:application/json'您发出的命令。

我遇到了完全相同的问题,并发现它与Spring Security有关。使用类似于以下内容的方法提高日志库中Spring Security的日志记录级别:

logging.level.org.springframework.security.web: DEBUG
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(ServerProperties.TRACING, "ALL");
this.addProperties(map);
之后,当您发送请求时,您可以看到在过滤器链上执行的最后一个过滤器是什么。在我的例子中,它是CsrfProtectionFilter。我禁用了它,它工作了

如果这没有帮助,还可以尝试启用REST框架的跟踪。这也可以为您提供线索。您可以通过以下方式在jersey中完成此操作:

logging.level.org.springframework.security.web: DEBUG
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(ServerProperties.TRACING, "ALL");
this.addProperties(map);
HashMap map=newhashmap();
map.put(ServerProperties.TRACING,“ALL”);
这是addProperties(map);

我遇到了完全相同的问题,并发现它与Spring Security有关。使用类似于以下内容的方法提高日志库中Spring Security的日志级别:

logging.level.org.springframework.security.web: DEBUG
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(ServerProperties.TRACING, "ALL");
this.addProperties(map);
之后,当您发送请求时,您可以看到在过滤器链上执行的最后一个过滤器是什么。在我的例子中,它是CsrfProtectionFilter。我禁用了它,它工作了

如果这没有帮助,还可以尝试启用REST框架的跟踪。这也可以为您提供线索。您可以通过以下方式在jersey中完成此操作:

logging.level.org.springframework.security.web: DEBUG
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(ServerProperties.TRACING, "ALL");
this.addProperties(map);
HashMap map=newhashmap();
map.put(ServerProperties.TRACING,“ALL”);
这是addProperties(map);

请详述您的答案。Hello Credo,我假设您的意思是在我的POST方法之前添加路径批注。我尝试了,现在我得到了HTTP/1.1 405方法不允许的错误…请详述您的答案。Hello Credo,我假设您的意思是在我的POST方法之前添加路径批注。我尝试了,现在我得到了HTTP/1.1 405方法不允许出错…(我认为DAO代码在这一点上不相关。更有用的是您正在使用的精确的curl命令,明确说明框架等。)@Srinivas,感谢您提供的信息。我的curl命令类似于:curl-i-X POST-H'内容类型:application/json'-d'{“firstname”:“John”,“lastname”:“Doe”}@california6586那么您在
GET
中使用的curl命令是什么?您好Srinivas,下面是我的GET curl命令,它工作得很好:curl-i-X GET(在这一点上DAO代码并不相关。更有用的是您正在使用的精确的curl命令,明确说明框架,等等)@Srinivas,感谢您提供的信息..我的curl命令是这样的:curl-i-X POST-H'内容类型:application/json'-d'{“firstname”:“John”,“lastname”:“Doe”}“@california6586那么您用于
GET
的curl命令是什么,它工作得很好?嗨,Srinivas,这是我的GET curl命令,它工作得很好:curl-i-X GET-Hi-Fud..感谢您的响应..我没有丢失内容类型:aplication/json..这是我的curl命令:curl-i-X POST-H'Content-Type:application/json'-d'{“firstname”:“John”,“lastname”:“Doe”}localhost:8080/persondb/rest/peopleHi Fud..感谢您的回复..我没有丢失内容类型:aplication/json..这是我的curl命令:curl-I-X POST-H'Content-Type:application/json'-d'{“firstname”:“John”,“lastname”:“Doe”}localhost:8080/persondb/rest/people