【发布时间】:2017-10-11 09:48:17
【问题描述】:
我需要创建一个使用 Post 方法的 Web 服务。
我正在使用的程序是:
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 + "]";
}
}
这就是我的模型的样子。 现在是我挣扎的部分。我有一个很好用的 get 方法,但是……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;
}
当我在 SoapUI 中运行我的 post 方法时,我在原始数据文件中收到此错误。
HTTP/1.1 400 错误请求 连接:关闭 日期:格林威治标准时间 2017 年 10 月 11 日星期三 06:55:51 内容长度:11 内容类型:文本/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 内部服务器错误 连接:关闭 日期:格林威治标准时间 2017 年 10 月 11 日星期三 07:01:51 内容长度:15 内容类型:文本/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 不支持的媒体类型 连接:关闭 日期:格林威治标准时间 2017 年 10 月 11 日星期三 06:59:42 内容长度:22 内容类型:文本/html;字符集=UTF-8 X-ORACLE-DMS-ECID:4b097786-3b8a-40f3-83c6-c337eb9db63e-000042dd X-ORACLE-DMS-RID:0
不支持的媒体类型
【问题讨论】:
标签: java eclipse rest post plsql