2017-04-10 12 views
0

パラメータが固定されていない数のパスを持つRESTサービスを作成したいとします。たとえば:JAX-RSのパラメータとしてパス名を取得する方法@Path

@Path("obj") 
public class ObjectResource { 

    @GET 
    @Path(???) 
    public Response getObj(@Param(???) String path) { 
     .... 
    } 
} 

リクエストURLが似ている場合:

http://myhost.xyz/app/obj/var/share/www 

方法getObjはそのパスパラメータとして文字列

var/share/www 

代わりに、それは次のようになりになるだろうOKをクリックして "var" "sharの配列(またはコレクション)を取得しますe "" www "を別々の順序付けられた要素で使用します。 (とにかくString.split()としてください)

これはできますか?

+1

[UriInfo](https://www.ibm.com/support/knowledgecenter/en/SSAW57_8.5.5/com.ibm。 websphere.nd.doc/ae/twbs_jaxrs_contextobjects_uri.html)はあなたの友人です –

+0

@RomanVottnerまあ、それはばかげて簡単でした。それを答えとして書きたいなら、私はそれをあなたのためにマークします。 – AlanObject

答えて

1

PathSegmentを試すことができます。

@Path("obj") 
public class ObjectResource { 

    @GET 
    @Path("{var: \\.+}") 
    public Response getObj(@PathParam("var") final PathSegment path) { 
     final String value = path.getPath(); 
    } 
} 

編集:何を実際に動作すると、以下の通りです:

@GET 
@Path("obj") 
public class ObjectResource {  

    @Path("{part: .*}") 
    @Produces(MediaType.APPLICATION_JSON) 
    public Response getObj(@PathParam("part") String pathpart) { 

     // just return the path 
     return Response.ok(pathpart).build(); 
    } 
} 
関連する問題