« Bondで1日縛られた土曜日 | トップページ | RestletにBasic認証を構成してみた »

2020年10月11日 (日)

RestletでのAnnotationの続き

日曜日にやっと時間がとれたのでRestletのAnnotationの続きを試してみた。

RestletのAnnotationはパラメータを指定できる。で、それってどのように動作するのか試してみた。

以下のコードを用意

public class DataInput extends ServerResource {

   @Post ("txt")
   public String represent(Representation entity) {

      String str = getReference().getPath();
      System.out.println( str );
      return "Post:txt "+str;
   }


   @Post ("json")
   public String representjson(Representation entity) {

      String str = getReference().getPath();
      System.out.println( str );
      return "Post:json "+str;
   }

}

Restクライアントでは、まずは以下のPythonコードを実行した。

   response=request.post('http://192.168.1.62:8010/command/datain/temp", headers={'Content-Type':'text/plain'})

responseは Post:txt /command/datain/temp

次に以下を実行した。

   response=request.post('http://192.168.1.62:8010/command/datain/temp",headers={'Content-Type':'application/json'})

responseは Post:json /command/datain/temp

つまり、同じPOSTでもContent-Typeで実行するコードを使い分けることができるわけだ。

これは便利な気がする。

« Bondで1日縛られた土曜日 | トップページ | RestletにBasic認証を構成してみた »

ラズパイ日記」カテゴリの記事

コメント

コメントを書く

(ウェブ上には掲載しません)

« Bondで1日縛られた土曜日 | トップページ | RestletにBasic認証を構成してみた »