0

現在、REST API に新しい機能を追加しようとしています。

基本的に、パスの最後にクエリ パラメーターを追加する機能を追加し、これをたとえばすべてのクエリ オプションのマップに変換したいと考えています。

私の現在のコードでは、次のようなことができます

localhost:8181/cxf/check/
localhost:8181/cxf/check/format
localhost:8181/cxf/check/a/b
localhost:8181/cxf/check/format/a/b

これにより、すべての @pathparam が文字列変数として使用され、応答が生成されます。

私が今やりたいことは、追加することです:

localhost:8181/cxf/check/a/b/?x=abc&y=def&z=ghi&...
localhost:8181/cxf/check/format/a/b/?x=abc&y=def&z=ghi&...

そして、これにより、pathparam と一緒に使用して応答を構築できる Map を生成します。

x => abc
y => def
z => ghi
... => ...

私はこの[下]のようなことを考えていましたが、@QueryParamは1つのキー値のみを処理し、それらのマップを処理していないようです。

@GET
@Path("/{format}/{part1}/{part2}/{query}")
Response getCheck(@PathParam("format") String format, @PathParam("part1") String part1, @PathParam("part2") String part2, @QueryParam("query") Map<K,V> query);

以下は私の現在のインターフェースコードです。

@Produces(MediaType.APPLICATION_JSON)
public interface RestService {

@GET
@Path("/")
Response getCheck();

@GET
@Path("/{format}")
Response getCheck(@PathParam("format") String format);

@GET
@Path("/{part1}/{part2}")
Response getCheck(@PathParam("part1") String part1,@PathParam("part2") String part2);

@GET
@Path("/{format}/{part1}/{part2}")
Response getCheck(@PathParam("format") String format, @PathParam("part1") String part1, @PathParam("part2") String part2);

}
4

1 に答える 1

1

QueryParam("") myBean注入されたすべてのクエリ パラメータを取得できます。最後の{query}部分も削除

@GET
@Path("/{format}/{part1}/{part2}/")
Response getCheck(@PathParam("format") String format, @PathParam("part1") String part1, @PathParam("part2") String part2, @QueryParam("") MyBean myBean);

 public class MyBean{
    public void setX(String x) {...}
    public void setY(String y) {...}  
 }

また、パラメーターを宣言して URI を解析することもできません。このオプションは、固定されていないパラメーターを受け入れることができる場合に役立ちます

 @GET
 @Path("/{format}/{part1}/{part2}/")
 public Response getCheck(@PathParam("format") String format, @PathParam("part1") String part1, @PathParam("part2") String part2, @Context UriInfo uriInfo) {
    MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
    String x= params.getFirst("x");
    String y= params.getFirst("y");
}
于 2016-12-12T12:41:08.627 に答える