gpt4 book ai didi

java - 在 JDeveloper 12c 中测试 Web 服务

转载 作者:行者123 更新时间:2023-12-02 06:46:37 25 4
gpt4 key购买 nike

我使用 JDeveloper 12c 制作了非常简单的 Web 服务。我有一个名为 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),即我不知道如何提供参数。这是屏幕截图:

Screenshot

我尝试单击绿色加号(添加 header ),然后将 header 名称更改为“名字”和“姓氏”并设置一些值,但是当我单击“发送请求”时,出现错误消息(名字或姓氏为空)。

输入某些输入参数的正确方法是什么?

最佳答案

FormParam 需要一个可以获取参数的表单。如果要对此进行测试,请在服务中将参数类型更改为 PathParam 或 QueryParam。这样,您就可以使用分析仪对其进行测试。因此,对于 QueryParam,您的 addPerson 将如下所示:

public void addPerson(@QueryParam("firstname") 字符串名字, @QueryParam("lastname") 字符串姓氏) {::}

在分析器中,您只需将 ?firstname=ABC&lastname=XYZ 附加到 URL 并发送请求即可。

关于java - 在 JDeveloper 12c 中测试 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18568435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com