0
私はRESTful Webサービスを作成しています。私が使用する技術:JerseyのカスタムメディアタイプのMessageBodyWriter
- (Javaの6に基づく)Glassfishの3
- JDK v7の
- のEclipse EEケプラー
- ジャージー(Glassfishのの一部)
私が作成しましたカスタムMediaTypeのカスタムPOJO:
public final class SimpleEntity{
private int value = 0;
private String stValue = "";
public SimpleEntity(){}
public SimpleEntity(int value, String stValue) {
super();
this.value = value;
this.stValue = stValue;
}
... getters, setters
私のリソースm ethod:
@POST
@Produces("application/entity")
@Path("/getSimpleEntityNeedsProvider")
public Response getSimpleEntityNeedsProvider(){
SimpleEntity entity = new SimpleEntity(47, "String input value");
return Response.ok().entity(entity).build();
}
マイメッセージ本文ライター:
@Provider
@Produces("application/entity")
public final class SimpleEntityBodyWriterProvider implements MessageBodyWriter<SimpleEntity>{
private static Logger log = Logger.getLogger(Class.class);
@Override
public long getSize(SimpleEntity arg0, Class arg1, Type arg2, Annotation[] arg3,
MediaType arg4) {
return -1;
}
@Override
public boolean isWriteable(Class arg0, Type arg1, Annotation[] arg2,
MediaType arg3) {
if(arg0.equals(SimpleEntity.class)){
return true;
}
return false;
}
@Override
public void writeTo(SimpleEntity entity, Class arg1, Type arg2, Annotation[] arg3,
MediaType media, MultivaluedMap arg5, OutputStream out)
throws IOException, WebApplicationException {
log.log(Level.INFO, "Input to SimpleEntityBodyWriterProvider: "+entity.getValue());
out.write(entity.getValue());
out.write(entity.getStValue().getBytes());
}
}
マイサービスクライアント:
private void getSimpleEntityNeedsProvider(String strUrl, String method){
HttpURLConnection connect = null;
try {
URL url = new URL(strUrl);
connect = (HttpURLConnection)url.openConnection();
connect.setRequestProperty("Accept", "application/entity");// Accept from server
connect.setRequestMethod(method);
connect.connect();
InputStream input = connect.getInputStream();
int size = input.available();
byte[] b = new byte[size];
input.read(b, 0, size);
System.out.println("From resource: "+new String(b));
System.out.println("Status: "+connect.getResponseCode()+" : "+connect.getResponseMessage());
}
catch(IOException e){
e.printStackTrace();
}
}
ルートパス:
@ApplicationPath("/rest/v6")
public class AppV6 extends Application {
private static Logger log = Logger.getLogger(Class.class.getName());
public AppV6(){}
@Override
public Set<Class<?>> getClasses(){
Set<Class<?>> cls = new HashSet<>();
cls.add(SimpleEntity.class);
cls.add(SimpleEntityBodyWriterProvider.class);
return cls;
}
}
私はアプリケーションを実行すると、私は私のサービスクライアントからの次の出力が得られます。
From resource: /String input value
Status: 200 : OK
私はRESTfulな/ジャージーサービスのより良い理解を持っているために、カスタムメディアタイプとMessageBodyWriter
を使用します。 私が持っている質問:
- これはカスタムメディアタイプの組み込み/コーディングの正しい方法ですか?
- サービスクライアントで受け取ったデータが完全ではありません。つまり、「/」の代わりに値は数字47である必要があります。だから、なぜ数字の代わりに文字を取得するのですか?
- リソースメソッドIで見たように、カスタムPOJO:digitとstringに2つの値を入力します。サービスクライアントでは私は
InputStream
を持っています。私は全体のデータのバイト配列を取得します。自分のPOJOでフィールドとしてエンコードされている別々のデータのまとまりとしてデータを読み取るにはどうすればよいですか?私は1バイトずつ読むことができましたが、正しい方法ですか?