2016-07-18 43 views
1

私はこのRESTリソースを持っている:例えば、6576/M982スラッシュ文字を含む文字列パスパラメータを渡すにはどうすればよいですか?

@GET 
@Path("{business},{year},{sample}") 
@Produces(MediaType.APPLICATION_JSON) 
public Response getSample(
     @PathParam("business") String business, 
     @PathParam("year") String year, 
     @PathParam("sample") String sampleId { 
    Sample sample = dao.findSample(business, year, sampleId); 
    return Response.ok(sample).build(); 
} 

sample paramがスラッシュ文字を含めることができます。

私はhttp://ip:port/samples/2000,2006,6576/M982と呼んでいますが、明らかに機能しません。

スラッシュを%2Fとしてエンコードしたhttp://ip:port/samples/2000,2006,6576%2FM982でも試しましたが、どちらも機能しません。エンドポイントには届きません。

EDIT

私は、エンドポイントを呼び出すためにレトロフィットを使用していると私はこれを行う:encoded = true

@GET("/samples/{business},{year},{sampleId}") 
Observable<Sample> getSampleById(
     @Path("business") String business, 
     @Path("year") String year, 
     @Path(value = "sampleId", encoded = true) String sampleId); 

、まだ動作していません。

+0

あなたは 'URLEncode'する必要があります。 – EJP

+0

'@GET("/samples/{business}%2C {year}%2C {sampleId} ")'を試したことがありますか? –

答えて

2

,/などの予約文字は、URLエンコードされている必要があります。 %2C

  • /%2F
  • としてエンコードされるよう

    • ,がエンコードされているhttp://ip:port/samples/2000%2C2006%2C6576%2FM982を試してみてください。


      RFC 3986は、区切り文字として使用することができるreserved characters次のセットを定義します。したがって、彼らはURLエンコードが必要です。

      :/? #/[ ]/@ ! $ & ' () * + , ; = 
      

      Unreserved charactersは、URLエンコードを必要としない:

      A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
      a b c d e f g h i j k l m n o p q r s t u v w x y z 
      0 1 2 3 4 5 6 7 8 9 - _ . ~ 
      

      URLエンコード,はあなたのために良い選択肢ではない場合、あなたはクエリパラメータを使用することを検討してください可能性があります。あなたのコードは次のようになります:

      @GET 
      @Produces(MediaType.APPLICATION_JSON) 
      public Response getSample(@QueryParam("business") String business, 
                @QueryParam("year") String year, 
                @QueryParam("sample") String sampleId { 
          ... 
      } 
      

      そして、あなたのURLはhttp://ip:port/samples?business=2000&year=2006&sample=6576%2FM982のようになります。

      /はまだURLエンコードされている必要があります。

    関連する問題