2012-03-25 21 views
2

私はRESTEasyを、より具体的にはフレームワークのクライアント側を使用しています。RESTサービスが間違ったコンテンツタイプとアンマーシャリングを返しました

JSONコードを返す3番目の部分Webサービスを呼び出しています。

しかし、いくつかの理由から、応答のコンテンツタイプは "text/javascript"です。

"text/javascript"コンテンツタイプに対してJSONプロバイダ(アンマーシャリング目的)を使用する必要があることをRESTEasyにどのように伝えることができますか?

これは可能ですか?

マイコード:

public interface XClient { 

@GET 
@Produces("application/json") 
@Path("/api/x.json") 
public Movie getMovieInformation(
     @QueryParam("q") String title); 
} 

ようなソリューションは何ができるか:私は時間が不足していますので、これは私のためのトリックをした

public interface XClient { 

@GET 
@Produces("text/javascript") 
// Tell somehow to use json provider despite the produces annotation 
@Path("/api/x.json") 
public Movie getMovieInformation(
     @QueryParam("q") String title); 
} 

答えて

0

。私は、文字列としてサーバからの応答をマークした、と私は手動でジャクソンとアンマーシャリングを処理しました:

public interface XClient { 

@GET 
@Path("/api/x.json") 
@Produces(MediaType.APPLICATION_JSON) 
public String getMovieInformation(
     @QueryParam("q") String title, 

} 

と、私のREST呼び出しで:これがない場合は

MovieRESTAPIClient client = ProxyFactory.create(XClient.class,"http://api.xxx.com"); 
String json_string = client.getMovieInformation("taken"); 

ObjectMapper om = new ObjectMapper(); 
Movie movie = null; 
try { 
    movie = om.readValue(json_string, Movie.class); 
} catch (JsonParseException e) { 
myLogger.severe(e.toString()); 
e.printStackTrace(); 
} catch (JsonMappingException e) { 
myLogger.severe(e.toString()); 
    e.printStackTrace(); 
} catch (IOException e) { 
    myLogger.severe(e.toString()); 
    e.printStackTrace(); 
} 

教えてくださいより良い解決策です。しかし、これは動作するようです。

1

私はこのように、着信コンテンツの種類を置き換えるインターセプタを使用することによって解決:

this.requestFactory.getSuffixInterceptors().registerInterceptor(
    new MediaTypeInterceptor()); 


static class MediaTypeInterceptor implements ClientExecutionInterceptor { 

    @Override 
    public ClientResponse execute(ClientExecutionContext ctx) throws Exception { 
     ClientResponse response = ctx.proceed(); 
     String contentType = (String) response.getHeaders().getFirst("Content-Type"); 
     if (contentType.startsWith("text/javascript")) { 
      response.getHeaders().putSingle("Content-Type", "application/json"); 
     } 
     return response; 
    } 

} 
+0

しかし、これは、すべての着信要求に影響? –

+1

はい、すべての回答です。ここではRESTクライアントについて説明しています。また、あなたが話しているサービスがJSONを 'text/javascript 'として返すならば、それは普通あなたが望むものです。クライアントはデフォルトでこのコンテンツタイプを処理できません。 – pdudits

関連する問題