curl 명령을 통해 POST 메서드에 JSON 문자열을 전달하려고합니다. 그러나 HTTP 1.1 404 찾을 수 없음 오류가 나타납니다. JSON 문자열을 전달하여 mysql db를 채울 간단한 웹 서비스를 만들려고합니다.curl을 사용하여 POST를 실행할 때 HTTP 404를 찾을 수 없음
GETS와 POSTS를 수행하는 데 사용되는 DAO 클래스입니다.
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 명령을 표. 그러나 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> [...]
내가 잘못 가고있는 부분을 알려주십시오.
(IMO의 DAO 코드는이 시점과 관련이 없습니다. 정확한 컬링 명령을 사용하면 프레임 워크에 대해 명시 적으로 더 유용 할 것입니다.) –
@Srinivas, 정보 주셔서 감사합니다. 내 말풍선 명령은 다음과 같습니다. curl -i -X POST -H 'Content-Type : application/json'-d '{ "firstname": "John", "lastname" : "Doe"} 'http : // localhost : 8080/persondb/rest/people – california6586
@ california6586 그리고 잘 작동하는'GET'에 사용하는 컬 명령은 무엇입니까? – Srinivas