使用 RESTful URL 传递不确定的查询参数并在 RESTEasy 中读取它们 [英] Passing indefinite Query Parameters with RESTful URL and reading them in RESTEasy

查看:44
本文介绍了使用 RESTful URL 传递不确定的查询参数并在 RESTEasy 中读取它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 RESTEasy 设计 RESTful 服务.客户端可以使用他们想要的任意数量的查询参数调用此公共服务.我的 REST 代码应该能够以某种方式读取这些查询参数.例如,如果我有图书搜索服务,客户可以拨打以下电话.

I have a requirement to design a RESTful Service using RESTEasy. Clients can call this common service with any number of Query Parameters they would want to. My REST code should be able to read these Query Params in some way. For example if I have a book search service, clients can make the following calls.

http://domain.com/context/rest/books/searchBook?bookName=someBookName
http://domain.com/context/rest/books/searchBook?authorName=someAuthor& pubName=somePublisher
http://domain.com/context/rest/books/searchBook?isbn=213243
http://domain.com/context/rest/books/searchBook?authorName=someAuthor

我必须编写一个如下所示的服务类来处理这个问题.

I have to write a service class like below to handle this.

@Path("/books")
   public class BookRestService{

    // this is what I currently have, I want to change this method to in-take all the 
    // dynamic parameters that can come
    @GET
    @Path("/searchBook")
    public Response searchBook(@QueryParam("bookName") String bookName,@QueryParam("isbn") String isbn) {

     // fetch all such params
     // create a search array and pass to backend

    }

    @POST
    @Path("/addBook")
    public Response addBook(......) {
    //....
     }
    }

抱歉格式不正确(我不知道代码格式在这个编辑器中是如何工作的!).如您所见,我需要更改方法 searchBook() 以便它接受任意数量的查询参数.

Sorry for the bad format (I couldn't get how code formatting works in this editor!). As you can see, I need to change the method searchBook() so that it will take any number of query parameters.

我在这里看到了类似的帖子,但找不到正确的解决方案.

I saw a similar post here, but couldn't find the right solution.

如何设计带有可选参数的用于搜索的 RESTful URL?

有人可以对此有所了解吗?

Could any one throw some light on this please?

推荐答案

在这种情况下,最好的做法是使用包含搜索条件所有字段的 DTO.例如,您提到了 4 个不同的参数.

The best thing to do in this case would be using a DTO containing all the fields of your search criteria. For example, you mentioned 4 distinct parameters.

  1. 书名(bookName)
  2. 作者姓名(authorName)
  3. 发布者名称(pubName)
  4. ISBN (isbn)

为您要将参数映射到的每个属性创建一个包含以下注释的字段的 DTO:

Create a DTO containing the fields having the following annotations for every property you want to map the parameters to:

public class CriteriaDTO{

  @QueryParam("isbn")
  private String isbn;
.
.

Other getter and setters of other properties

}

这里有一个方法供您参考:

Here is a method doing that for your reference:

@GET
@Produces("application/json")
@Path("/searchBooks")
public ResultDTO search(@Form CriteriaDTO dto){
}

使用以下 URL 将自动填充 CriteriaDTO 的属性 isbn:

using following URL will populate the CriteriaDTO's property isbn automatically:

your.server.ip:port/URL/Mapping/searchBooks?isbn=123456789&pubName=testing

your.server.ip:port/URL/Mapping/searchBooks?isbn=123456789&pubName=testing

这篇关于使用 RESTful URL 传递不确定的查询参数并在 RESTEasy 中读取它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆