我使用 JDeveloper 12c 制作了非常简单的网络服务。 我有一个名为Person的课程:
package testapp;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Person {
private String firstname;
private String lastname;
public Person() {super();}
public Person(String firstname, String lastname) this.firstname=firstname;this.lastname=lastname;}
public void setFirstname(String firstname) {this.firstname = firstname;}
public String getFirstname() {return firstname;}
public void setLastname(String lastname) {this.lastname = lastname;}
public String getLastname() {return lastname;}
}
...和一个PersonService类:
package testapp;
import java.util.ArrayList;
import java.util.List;
import java.sql.*;
public class PersonService {
private List<Person> persons;
public PersonService() {
super();
this.persons = new ArrayList<Person>();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("ERROR: Cannot find Oracle driver!");
e.printStackTrace();
return;
}
System.out.println("STATUS: Oracle JDBC Driver Registered!");
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = DriverManager.getConnection(/* my database connection parameters */);
} catch (SQLException e) {
System.out.println("ERROR: Connection Failed! Check output console!");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("STATUS: Connection established.");
} else {
System.out.println("ERROR: Failed to establish connection!");
}
try {
String selectSQL = "/* select first and last name from database */";
preparedStatement = connection.prepareStatement(selectSQL);
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
this.persons.add(/* Get data from database and make new Person */);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
try {
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
System.out.println("ERROR: Something is seriously wrong...");
}
}
}
public List<Person> getPersons() {return this.persons;}
public void addPerson(String firstname, String lastname) {
if (firstname != null && lastname != null) {
System.out.println("Hello, "+firstname+" "+lastname+"!");
} else {
System.out.println("ERROR: First name or last name is null!");
}
}
}
我已关注this tutorial制作网络服务并对其进行测试。我选择了application / xml和application / json类型的参数。最后,我的addPerson方法如下所示:
@POST
@Consumes(value = { "application/json", "application/xml" })
@Path("/person")
public void addPerson(@FormParam("firstname") String firstname, @FormParam("lastname") String lastname) {
if ((firstname != null) && (lastname != null)) {
System.out.println("Hello, "+firstname+" "+lastname+"!");
} else {
System.out.println("ERROR: First name or last name is null!");
}
}
我能够测试我的GET方法(getPersons),并且我得到一个漂亮的响应。但我不知道如何测试POST方法(addPerson),即我不知道如何提供参数。这是截图:
我尝试点击绿色加号(添加标题),然后将标题名称更改为&#34; firstname&#34;和&#34;姓氏&#34;并设置一些值,但是当我点击&#34;发送请求&#34;时,会出现一条错误消息(名字或姓氏为空)。
输入一些输入参数的正确方法是什么?
答案 0 :(得分:1)
您必须在发送请求正上方的栏中提供名字和姓氏 按键 以下列方式
姓名=弗拉基米尔·&安培;姓=补药
然后单击“发送请求”按钮
答案 1 :(得分:1)
FormParam需要一个表单,通过该表单可以获取参数。如果要对此进行测试,请在服务中将参数类型更改为PathParam或QueryParam。这样,您可以使用分析仪进行测试。 因此,在QueryParam的情况下,您的addPerson将如下所示:
public void addPerson(@QueryParam(&#34; firstname&#34;)String firstname,@ QueryParam(&#34; lastname&#34;)String lastname){ : : }
在分析器中,您可以将?firstname = ABC&amp; lastname = XYZ附加到URL并发送请求。