2012-12-27 3 views
1

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> [...] 

내가 잘못 가고있는 부분을 알려주십시오.

+2

(IMO의 DAO 코드는이 시점과 관련이 없습니다. 정확한 컬링 명령을 사용하면 프레임 워크에 대해 명시 적으로 더 유용 할 것입니다.) –

+0

@Srinivas, 정보 주셔서 감사합니다. 내 말풍선 명령은 다음과 같습니다. curl -i -X ​​POST -H 'Content-Type : application/json'-d '{ "firstname": "John", "lastname" : "Doe"} 'http : // localhost : 8080/persondb/rest/people – california6586

+0

@ california6586 그리고 잘 작동하는'GET'에 사용하는 컬 명령은 무엇입니까? – Srinivas

답변

2

POST before 메서드에서 PATH를 추가하고 시도해보십시오.

+0

답변을 확장하십시오. –

+0

안녕하세요 Credo, 내 POST 메서드 전에 PATH 주석을 추가한다는 것을 의미한다고 가정했습니다. 나는 그것을 시도하고 이제 HTTP/1.1 405 Method Not Allowed 오류가 발생한다 ... – california6586

1

귀하가 발행 한 명령에 -H 'Content-Type : application/json'이 누락되어 있어야한다고 가정합니다.

+0

안녕하세요 Food .. 감사합니다. .. Content-Type : aplication/json ..이 누락 된 것은 아닙니다. 이것은 내 컬 명령입니다. curl -i -X ​​POST -H 'Content-Type : application/json'-d '{ "firstname": "John", "lastname": "Doe"}'localhost : 8080/persondb/rest/people – california6586

1

나는 똑같은 문제가 있었고 스프링 보안과 관련이 있다는 것을 알았습니다. 뭔가 다음과 비슷한 로깅 라이브러리에 봄 보안의 로깅 수준을 높입니다 : 그 후

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

을, 당신은 당신이 필터 체인에서 마지막으로 실행 된 필터 무엇인지 볼 수 있습니다 귀하의 요청을 보낼 때. 제 경우에는 CsrfProtectionFilter였습니다. 나는 그것을 작동시키지 않았고 작동했다.

그래도 도움이되지 않으면 REST 프레임 워크 추적을 사용하도록 설정해보십시오. 그것은 또한 당신에게 단서를 줄 수 있습니다.

HashMap<String, Object> map = new HashMap<String, Object>(); 
map.put(ServerProperties.TRACING, "ALL"); 
this.addProperties(map); 
관련 문제