java - 传递查询参数rest客户端

标签 java rest jersey

我有一个休息服务,可以从数据库检索数据并将其返回给客户端。我希望调用服务的客户端传递参数,以便在 sql 查询 select 中使用它们,并在控制台中显示服务器输出。这就是我设法做到的:

@GET
    @Path("Result")
    @Produces("application/json")
    public String getPerson(@QueryParam("nom") String nom, @QueryParam("prenom") String prenom) {
        ArrayList <Persons> persons= new ArrayList<Persons>();
        Persons person = new Persons();

        String query = "Select * from persons where nom=' " + nom + "' and prenom ='" + prenom + "'";
        System.out.println(query);
        bdcon = new BDConnexion();
        try {
            conn = BDConnexion.ConnecterBD();
            res = bdcon.getResultSet(query, conn);
            while (res.next()) {
                person.setNom(res.getString(1));
                person.setPrenom(res.getString(2));
                persons.add(person);
            }
        } catch (SQLException ex) {
            Logger.getLogger(PersonService.class.getName()).log(Level.SEVERE, null, ex);
        }
        String json = new Gson().toJson(persons);
        return json;
    }

休息客户端:

Client client = Client.create();
WebResource webresource = client.resource("http://localhost:8080/PersonServ/rest/Persons/Result")
.queryParam("nom", nom)
.queryParam("prenom",prenom);
ClientResponse response = webresource.accept("application/json").get(ClientResponse.class);
 if (response.getStatus() != 200) {
               throw new RuntimeException("Failed : HTTP error code : "
                + response.getStatus());
            }
            String output = response.getEntity(String.class);

            System.out.println("Output from Server .... \n");
            System.out.println(output);

我没有收到任何错误,但客户端类没有显示任何结果。谁能帮我吗?

最佳答案

正如评论中所讨论的,实际问题出在查询中。还有一些问题需要修复。

第一:

String query = "Select * from persons where nom=' " + nom + "' and prenom ='" + prenom + "'";
                                                 ^
                                                 |_ There is an extra space here. Take it out

但这只是为了向您表明您应该注意在查询中连接参数所带来的问题。

第二:您的代码很容易出现 SQLInjection正如@peeskillet 在评论中提到的。为了避免这种情况,您应该使用准备好的语句,如下所示:

conn = BDConnexion.ConnecterBD();
String selectSQL = "select * from persons where nom=? and prenom=?";
PreparedStatement preparedStatement = conn.prepareStatement(selectSQL);
preparedStatement.setString(1, nom);
preparedStatement.setString(2, prenom);
ResultSet rs = preparedStatement.executeQuery(selectSQL);
while (rs.next()) {
   ....

不要忘记关闭 tryfinnaly block 上的资源和连接

第三:在 while 循环内初始化 Persons person = new Persons();。 Java 使用引用,因此在循环之外实例化它,您将得到一个充满指向同一引用的对象的列表,这将导致列表中的所有对象具有相同的值(循环的最后一个)。

关于java - 传递查询参数rest客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37791601/

相关文章:

rest - ALPS 示例实现

java - 具有多个同名参数的 JAX-RS 查询

java - 使用 Jackson 添加超媒体以表示资源 JSON

java - JAXB 和 Jersey - 返回扩展抽象类的对象列表

java - @DELETE : "Unsupported HTTP method: DELETE" 上的 Jersey 错误

java - SpringBoot v1.5.14.RELEASE - 从测试资源文件夹读取文件

java - 如何在 C# 中使用 WebDriver 获取指定元素的屏幕截图

java - 带有 Spring-Boot-Starter-Parent 的多模块 Maven 项目结构

java - 为什么 JDK 默认不包含 JMS?

web-services - 基于操作的 URI 和基于资源的 URI 之间有什么区别?