[英]Rest API with a context object as the parameter
我正在使用JAX-RS設計REST API。 端點如下所示:
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response get(
@QueryParam("param1") final String param1,
@QueryParam("param2") final String param2) {
// Code goes here
}
我有將近7-8個參數。 因此,我想執行以下操作:
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response get(@Context MyContextObject context) {
// Code goes here
}
我有如下的上下文對象:
final class MyContextObject {
// get methods
public MyContextObject(final Builder builder) {
// set final fields
}
private final String param1;
private final String param2;
public static final class Builder {
// builder code goes here
}
}
您能告訴我我該怎么做嗎?
提前致謝。
如果要按照您所說的那樣創建單獨的bean類,則需要在bean類中獲取查詢參數,如下所示。
final class MyContextObject {
// get methods
public MyContextObject(final Builder builder) {
// set final fields
}
private @QueryParam("param1") final String param1;
private @QueryParam("param2") final String param2;
//and so on...
public static final class Builder {
// builder code goes here
}
}
如果這樣做,查詢參數將被綁定到Bean類中的這些私有變量,並且可以使用getter將它們獲取到rest服務中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.