2017-10-11 11 views
0

Postメソッドを使用するWebサービスを作成する必要があります。私が使用していますEclipse Webサービス(Post)が動作しない

プログラムは次のとおりです。
PLSQL開発 - これは私がにデータを投稿したい私のデータベースです。
Eclipse - これは私のWebサービスをコード化するために使用している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 + "]"; 
} 

} 

これは私のモデルの外観です。 今私が苦労している部分です。私は本当に良い作品getメソッドを持っていますが、私はSOAPUIで私のポストメソッドを実行すると、その後..... POST ..........

@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日6時55分51秒GMT のContent-Length:11 のContent-Type:text/htmlの。文字セット= UTF-8 X-ORACLE-DMS-ECID:4b097786-3b8a-40f3-83c6-c337eb9db63e-000042d8 X-ORACLE-DMS-RID:0

不正な要求

それではI私が持っている私の別のテーブルのための別の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 Oct 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素晴らしい作品です。 –

関連する問題