我正在使用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
}
}你能告诉我如何做到这一点吗?
提前谢谢。
发布于 2018-03-16 17:47:00
如果您希望像您所说的那样创建单独的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服务中获取它们。
https://stackoverflow.com/questions/22455127
复制相似问题