2017-10-11 1 views
0

Post 메서드를 사용하는 웹 서비스를 만들어야합니다. 내가 사용하고Eclipse 웹 서비스 (Post)가 작동하지 않음

프로그램은 다음과 같습니다
PLSQL 개발자 - 이것은 내가에 데이터를 게시 할 내 데이터베이스입니다.
Eclipse - 이것은 내 웹 서비스를 코딩하는 데 사용하는 Java 프로그램입니다.
SoapUI -이 프로그램은 내 rest 메서드를 배포하는 데 사용하는 프로그램입니다.

나는 몇 가지 포스트 메소드를 시도했는데 모두 실패했다.

public class AgentDAO { 


public List<Agent> selectAgents() throws SQLException { 

    Connection dbConnection = null; 
    PreparedStatement statement = null; 

    List<Agent> agents = new ArrayList<Agent>(); 

    String selectTableSQL = "SELECT * from AGENTS"; 

    try { 

     dbConnection = getDBConnection(); 
     statement = dbConnection.prepareStatement(selectTableSQL); 


     System.out.println(selectTableSQL); 

     // execute select SQL statement 
     ResultSet rs = statement.executeQuery(); 

     while (rs.next()) { 

      Agent agent = new Agent(); 
      agent.setAgentId(rs.getBigDecimal("AGENT_ID")); 
      agent.setName(rs.getString("FNAME")); 
      agent.setLastName(rs.getString("LNAME")); 
      agent.setEmail(rs.getString("EMAIL")); 
      agent.setDepartment(rs.getString("DEPARTMENT")); 
      agent.setCountry(rs.getString("COUNTRY")); 

      agents.add(agent); 

     } 

    } catch (SQLException e) { 

     System.out.println(e.getMessage()); 


    } finally { 

     if (statement != null) { 
      statement.close(); 
     } 

     if (dbConnection != null) { 
      dbConnection.close(); 
     } 

    } 

    return agents; 

} 

public void updateAgent(Agent agent) throws SQLException { 
    System.out.println("Method update"); 
    Connection dbConnection = null; 
    PreparedStatement statement = null; 
    String updateTableSQL = "UPDATE AGENTS" + " SET FNAME = ?, " + " LNAME 
    = ?, " + " DEPARTMENT = ?, " 
      + " EMAIL = ?, " + " COUNTRY = ? " + " WHERE AGENT_ID = ?"; 

    try { 

     dbConnection = getDBConnection(); 
     statement = dbConnection.prepareStatement(updateTableSQL); 
     statement.setString(1, agent.getName()); 
     statement.setString(2, agent.getLastName()); 
     statement.setString(3, agent.getDepartment()); 
     statement.setString(4, agent.getEmail()); 
     statement.setString(5, agent.getCountry()); 
     statement.setBigDecimal(6, agent.getAgentId()); 


     System.out.println(updateTableSQL); 
     // execute update SQL statement 
     statement.executeUpdate(); 


    } catch (SQLException e) { 

     System.out.println(e.getMessage()); 


    } finally { 

     if (statement != null) { 
      try { 
       statement.close(); 
      } catch (SQLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

     if (dbConnection != null) { 
      try { 
       dbConnection.close(); 
      } catch (SQLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

public void deleteAgent(Agent agent) throws SQLException { 

    Connection dbConnection = null; 
    PreparedStatement statement = null; 

    String deleteTableSQL = "DELETE AGENTS WHERE AGENT_ID = ?"; 

    try { 

     dbConnection = getDBConnection(); 
     statement = dbConnection.prepareStatement(deleteTableSQL); 
     statement.setBigDecimal(1, agent.getAgentId()); 

     System.out.println(deleteTableSQL); 

     // execute delete SQL statement 
     statement.execute(); 

     System.out.println("Record is deleted from AGENTS table!"); 

    } catch (SQLException e) { 

     System.out.println(e.getMessage()); 


    } finally { 

     if (statement != null) { 
      statement.close(); 
     } 

     if (dbConnection != null) { 
      dbConnection.close(); 
     } 

    } 

} 

public void insertAgent(Agent agent) throws SQLException { 

    Connection dbConnection = null; 
    PreparedStatement statement = null; 

    String insertTableSQL = "INSERT INTO AGENTS" + "(AGENT_ID, FNAME, LNAME, DEPARTMENT, EMAIL, COUNTRY) " 
      + "VALUES" + "(?,?,?,?,?,?)"; 

    try { 


     TimeZone testtime = TimeZone.getTimeZone("GMT+2"); 
     TimeZone.setDefault(testtime); 
     dbConnection = getDBConnection(); 
     statement = dbConnection.prepareStatement(insertTableSQL); 

     statement.setBigDecimal(1, agent.getAgentId()); 
     statement.setString(2, agent.getName()); 
     statement.setString(3, agent.getLastName()); 
     statement.setString(4, agent.getDepartment()); 
     statement.setString(5, agent.getEmail()); 
     statement.setString(6, agent.getCountry()); 


     System.out.println(insertTableSQL); 

     // execute insert SQL statement 

     statement.executeUpdate(); 
     // logger.info("AgentDAO - END"); 
     System.out.println("Record is inserted into AGENTS table!"); 

    } catch (SQLException e) { 

     System.out.println(e.getMessage()); 


    } finally { 

     if (statement != null) { 
      statement.close(); 
     } 

     if (dbConnection != null) { 
      dbConnection.close(); 
     } 

    } 

} 

private static Connection getDBConnection() { 

    Connection dbConnection = null; 

    try { 

     Class.forName(DB_DRIVER); 

    } catch (ClassNotFoundException e) { 

     System.out.println(e.getMessage()); 

    } 

    try { 

     dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); 
     return dbConnection; 

    } catch (SQLException e) { 

     System.out.println(e.getMessage()); 


    } 

    return dbConnection; 

} 

} 

이것은 내 plsql 데이터베이스에 데이터를 선택, 업데이트, 삭제 및 추가하는 코드입니다.

public class Agent { 

private BigDecimal agentId; 
private String name; 
private String lastName; 
private String department; 
private String email; 
private String country; 

public BigDecimal getAgentId() { 
    return agentId; 
} 
public void setAgentId(BigDecimal agentId) { 
    this.agentId = agentId; 
} 
public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
public String getLastName() { 
    return lastName; 
} 
public void setLastName(String lastName) { 
    this.lastName = lastName; 
} 
public String getDepartment() { 
    return department; 
} 
public void setDepartment(String department) { 
    this.department = department; 
} 
public String getEmail() { 
    return email; 
} 
public void setEmail(String email) { 
    this.email = email; 
} 
public String getCountry() { 
    return country; 
} 
public void setCountry(String country) { 
    this.country = country; 
} 

@Override 
public String toString() { 
    return "Agent [agentId=" + agentId + ", name=" + name + ", lastName=" + lastName + ", department=" + department 
      + ", email=" + email + ", country=" + country + "]"; 
} 

} 

내 모델의 모습입니다. 지금 내가 고심하고있는 부분을 위해. 나는 ..... 다음 POST를 정말 잘 작동하지만 get 메소드 .......... 내가 SoapUI에 내 게시물의 방법을 실행하면

@GET 
@Produces(MediaType.APPLICATION_XML) 

public List<Agent> selectAgents() throws SQLException { 
    return agents.selectAgents(); 
} 

@POST 
@Consumes(MediaType.APPLICATION_XML) 
@Produces(MediaType.APPLICATION_XML) 
public String insertAgent(Agent agent) { 


    try { 
     agents.insertAgent(agent); 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return null; 
} 

@GET 
@Path("/{agentid}") 
@Produces(MediaType.APPLICATION_XML) 
public List<Agent> getAgent(@PathParam("agentid") BigDecimal id) { 

    try { 
     return agents.selectAgents(); 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return null; 
} 

내가 원시에서이 오류가 데이터 파일.

HTTP/1.1 400 잘못된 요청 연결 : 가까운 날짜 : 2017년 10월 11일 (수) 그리니치 표준시 06시 55분 51초 콘텐츠 길이 : 11 콘텐츠 형식 : text/html; 문자셋 = UTF-8 X-ORACLE-DMS-ECID : 4b097786-3b8a-40f3-83c6-c337eb9db63e-000042d8 X-ORACLE-DMS-RID : 0

잘못된 요청

그래서 나는 내가 가진 다른 테이블에 대해 다른 POST 메서드를 시도하고 다른 오류가 발생했습니다.

@POST 
@Produces(MediaType.APPLICATION_XML) 
public String insertComment() { 
    return "Post works!"; 
} 

나는 내가 실제로 다시 적어도 뭔가를 얻을 것입니다 있는지 확인하고 싶어하지만 나는이 다시있어 :

HTTP/1.1 500 내부 서버 오류 연결 : 가까운 날짜 : 수요일, 11 Oct 2017 07:01:51 GMT 콘텐츠 길이 : 15 콘텐츠 형식 : text/html; 문자셋 = UTF-8 X-ORACLE-DMS-ECID : 4b097786-3b8a-40f3-83c6-c337eb9db63e - 000042e0 X-ORACLE-DMS-RID는 0

요청이 실패했습니다.

은 그 때 나는
@POST 
@Consumes(MediaType.APPLICATION_XML) 
@Produces(MediaType.APPLICATION_XML) 
public Response insertCustomer(Customer customer) throws URISyntaxException 
{ 
    if(customer == null){ 
     return Response.status(400).entity(" Insert details !!").build(); 
    } 

    if(customer.getFname() == null) { 
     return Response.status(400).entity("Enter First name !!").build(); 
    } 

    return Response.created(new 
    URI("/customerid"+customer.getCustId())).build(); 
    } 

을 시도 그리고 오류

HTTP/1

을 얻었다.1 415 지원되지 않는 미디어 유형 연결 : 닫기 날짜 : 수요일, 11 10 월 2017 06:59:42 GMT 콘텐츠 길이 : 22 콘텐츠 형식 : text/html; 문자셋 = UTF-8 X-ORACLE-DMS-ECID : 4b097786-3b8a-40f3-83c6-c337eb9db63e-000042dd X-ORACLE-DMS-RID : 0

지원되지 않는 미디어 유형

답변

0

이 시도 코드

@POST 
@Path("create2") 
@Consumes("application/json") 
public String create(Agent entity) throws SQLException { 

    AgentDAO agents = new AgentDAO(); 
    agents.insertAgent(entity); 
    return "Successfully created"; 
} 
+0

KingCarlitos 덕분에 멋진 작품입니다. –