4

私の関数は次のようになります。

    @PUT
    @Path("property/{uuid}/{key}/{value}")
    @Produces("application/xml")    
    public Map<String,ValueEntity> updateProperty(@Context HttpServletRequest request,
            @PathParam("key") String key,
            @PathParam("value") String value,
            @PathParam("uuid") String uuid) throws Exception {
                                       ...
                             }

私はそれを変更する必要があるので、REST 呼び出しからのキーと値のペアの不定の (または多くの) リストを受け入れます。

@Path("property/{uuid}/{key1}/{value1}/{key2}/{value2}/{key3}/{value3}/...")

それらを配列またはリストに格納することは可能ですか。これを避けるために、数十の @PathParams とパラメーターをリストしません。

@PathParam("key1") String key1,
@PathParam("key2") String key2,
@PathParam("key3") String key3,
4

2 に答える 2

6

このデザインを再考する良い機会かもしれません。sを使用/することで、ある意味では、それぞれ/が異なるリソースを見つけようとしていることを意味しています。キーと値のペア (URL のコンテキストで) は、主にクエリ パラメーターまたはマトリックス パラメーター用です。

/property/{uuid}がメイン リソースへのパスであり、このリソースにアクセスするためのパラメーターをクライアントに提供したい場合は、マトリックス パラメーターまたはクエリ パラメーターを許可できます。

マトリックス パラメータ(リクエスト URL 内) は次のようになります。

/12345;key1=value1;key2=value2;key3=value3

値を取得するためのリソース メソッドは次のようになります。

@GET
@Path("/property/{uuid}")
public Response getMatrix(@PathParam("uuid") PathSegment pathSegment) {
    StringBuilder builder = new StringBuilder();

    // Get the {uuid} value
    System.out.println("Path: " + pathSegment.getPath());

    MultivaluedMap matrix = pathSegment.getMatrixParameters();
    for (Object key : matrix.keySet()) {
        builder.append(key).append(":")
               .append(matrix.getFirst(key)).append("\n");
    }
    return Response.ok(builder.toString()).build();
}

クエリ パラメータ(リクエスト URL 内) は次のようになります。

/12345?key1=value1&key2=value2&key3=value3

値を取得するためのリソース メソッドは次のようになります。

@GET
@Path("/property/{uuid}")
public Response getQuery(@PathParam("uuid") String uuid, 
                         @Context UriInfo uriInfo) {

    MultivaluedMap params = uriInfo.getQueryParameters();
    StringBuilder builder = new StringBuilder();
    for (Object key : params.keySet()) {
        builder.append(key).append(":")
               .append(params.getFirst(key)).append("\n");
    }
    return Response.ok(builder.toString()).build();
}

違いは、Matrix パラメーターはパス セグメントに埋め込むことができるのに対し、クエリ パラメーターは URL の末尾に配置する必要があることです。また、構文のわずかな違いにも気付くでしょう。


いくつかのリソース


アップデート

また、PUTメソッドの署名を見ると、更新しようとしている値としてパスを使用してリソースを更新しようとしているようです。エンティティ本体のメソッドにパラメーターが表示されないためです。PUTting するときは、パス セグメントやパラメーターとしてではなく、エンティティ本体で表現を送信する必要があります。

于 2014-11-18T11:35:33.830 に答える
4

回避策:

@Path("/foo/bar/{other: .*}
public Response foo(@PathParam("other") VariableStrings vstrings) {

   String[] splitPath = vstrings.getSplitPath();


}

変数文字列クラス:

public class VariableStrings {

   private String[] splitPath;

   public VariableStrings(String unparsedPath) {
     splitPath = unparsedPath.split("/");
   }
}

JAX-RS / Jerseyのvararg配列へのパスセグメントシーケンス?

オプションのパラメーターを Map にマップする別の例:

@GET
@ Produces({"application/xml", "application/json", "plain/text"})
@ Path("/location/{locationId}{path:.*}")
public Response getLocation(@PathParam("locationId") int locationId, @PathParam("path") String path) {
    Map < String, String > params = parsePath(path);
    String format = params.get("format");
    if ("xml".equals(format)) {
        String xml = "<location<</location<<id<</id<" + locationId + "";
        return Response.status(200).type("application/xml").entity(xml).build();
    } else if ("json".equals(format)) {
        String json = "{ 'location' : { 'id' : '" + locationId + "' } }";
        return Response.status(200).type("application/json").entity(json).build();
    } else {
        String text = "Location: id=" + locationId;
        return Response.status(200).type("text/plain").entity(text).build();
    }
}

private Map < String, String > parsePath(String path) {
    if (path.startsWith("/")) {
        path = path.substring(1);
    }
    String[] pathParts = path.split("/");
    Map < String, String > pathMap = new HashMap < String, String > ();
    for (int i = 0; i < pathParts.length / 2; i++) {
        String key = pathParts[2 * i];
        String value = pathParts[2 * i + 1];
        pathMap.put(key, value);
    }
    return pathMap;
}
于 2014-11-18T10:52:07.673 に答える