eclipse中的其他Web服务(Post)不起作用
我需要创建一个使用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
不支持的媒体类型
尝试此代码
@POST
@Path("create2")
@Consumes("application/json")
public String create(Agent entity) throws SQLException {
AgentDAO agents = new AgentDAO();
agents.insertAgent(entity);
return "Successfully created";
}
KingCarlitos感谢它的工作真棒。 –