1

我正在尝试通过 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> [...]

请让我知道我哪里出错了。

4

3 回答 3

3

I assume you must be missing -H 'Content-Type:application/json' in the command you issue.

于 2012-12-27T04:26:56.677 回答
2

I had exactly same problem and figured out it's related to Spring Security. Increase the logging level of spring security in your logging library with something similar to following:

logging.level.org.springframework.security.web: DEBUG

After that, when you send your request you can see what is the last filter executed on the filter chain. In my case it was CsrfProtectionFilter. I disabled it and it worked.

If that doesn't help, also try enabling the tracing of your REST framework. That can also give you a clue. You can do it in jersey by following:

HashMap<String, Object> map = new HashMap<String, Object>();
map.put(ServerProperties.TRACING, "ALL");
this.addProperties(map);
于 2014-08-15T09:02:17.147 回答
2

在 POST before 方法中,添加 PATH,尝试一下。

于 2012-12-27T01:45:42.617 回答