Reputation: 79
I am trying to pass a JSON string to a POST method via a curl command. However, i am receiving a HTTP 1.1 404 Not Found error. I am trying to create a simple web service which will have the JSON string passed to it to populate a mysql db.
Here is my DAO class which is used to do the GETS and the 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;
}
My POJO
@XmlRootElement
public class Person{
private Integer id;
private String firstName;
private String lastName;
//GETTERS AND SETTERS
}
My Person Resource class for my annotations:
@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);
}
When i issue the GET curl command it works fine and It returns all the values in the person table. However, if I issue a POST curl command I get the following error :
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> [...]
Kindly let me know where I am going wrong.
Upvotes: 1
Views: 7711
Reputation: 1297
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);
Upvotes: 2
Reputation: 5184
I assume you must be missing -H 'Content-Type:application/json' in the command you issue.
Upvotes: 3