我需要創建一個使用Post方法的Web服務。eclipse中的其他Web服務(Post)不起作用
我使用的程序是:
PLSQL開發 - 這是我的數據庫,我想將數據發佈到。
Eclipse - 這是我用來編寫我的web服務的java程序。
SoapUI - 這是我用來部署我的休息方法的程序。
我嘗試了一些Post方法,他們都失敗了我。
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 + "]";
}
}
這就是我的模型的樣子。 現在是我掙扎的部分。我的作品確實不錯,但隨後.....立柱A get方法..........
@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;
}
當我了SoapUI跑我的崗位我的方法在原料得到這個錯誤數據文件。
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
錯誤的請求
於是我爲我的不同表格嘗試了不同的POST方法,並且我得到了不同的錯誤。
@POST
@Produces(MediaType.APPLICATION_XML)
public String insertComment() {
return "Post works!";
}
我想看看我是否會真正得到至少東西回來,但後來我得到這個回:
HTTP/1.1 500內部服務器錯誤 連接:關閉 日期:週三,11 2017年10月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 Unsupported Media Type Connection:close Date:Wed,11 Oct 2017 06:59:42 GMT Content-Length:22 Content-Type:text/html;字符集= UTF-8 X-ORACLE-DMS-ECID:4b097786-3b8a-40f3-83c6-c337eb9db63e-000042dd X-ORACLE-DMS-RID:0
不支持的媒體類型
KingCarlitos感謝它的工作真棒。 –