has*_*der 3 resteasy url-parameters
我需要使用RESTEasy设计RESTful服务.客户端可以使用他们想要的任意数量的查询参数来调用此公共服务.我的REST代码应该能够以某种方式读取这些查询参数.例如,如果我有书籍搜索服务,客户可以进行以下调用.
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
我必须写一个像下面这样的服务类来处理这个问题.
@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(),以便它可以使用任意数量的查询参数.
我在这里看到了类似的帖子,但找不到合适的解决方案.
请问有人对此有所了解吗?
在这种情况下,最好的办法是使用包含搜索条件所有字段的DTO.例如,您提到了4个不同的参数.
创建一个DTO,其中包含要将参数映射到的每个属性具有以下注释的字段:
public class CriteriaDTO{
  @QueryParam("isbn")
  private String isbn;
.
.
Other getter and setters of other properties
}
这是一个方法,供您参考:
@GET
@Produces("application/json")
@Path("/searchBooks")
public ResultDTO search(@Form CriteriaDTO dto){
}
使用以下URL将自动填充CriteriaDTO的属性isbn:
your.server.ip:端口/ URL /映射/ searchBooks ISBN = 123456789&pubName =测试
| 归档时间: | 
 | 
| 查看次数: | 8030 次 | 
| 最近记录: |