REST 服务返回错误的内容类型并解组 [英] REST Service returning wrong content-type and unmarshall

查看:36
本文介绍了REST 服务返回错误的内容类型并解组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RESTEasy,更具体地说,是他们框架的客户端.

I'm using RESTEasy and more specifically, the client side of their framework.

我正在调用第三方 Web 服务,该服务返回一些 JSON 代码.

I'm calling a third part web service that's returning me some JSON code.

但是,出于某些充分的原因,他们响应中的内容类型是text/javascript".

But, for some good reasons, the content-type in their response is "text/javascript".

我如何告诉 RESTEasy 它应该使用 JSON 提供程序(解组目的)用于文本/javascript"内容类型?

How can I tell RESTEasy that it should be using a JSON provider (unmarshalling purposes) for a "text/javascript" content-type ?

这可能吗?

我的代码:

public interface XClient {  

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

解决方案可能是什么样的:

What an solution could be like :

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);
}

推荐答案

我的时间不多了,所以这对我有用.我已经将来自服务器的响应标记为字符串,并且我已经使用 Jackson 手动处理了解组:

I'm running out of time, so this did the trick for me. I've marked the response from the server as String, and I've manually handle the unmarshall with Jackson :

public interface XClient {  

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

}

并且,在我的 REST 调用中:

and, in my REST call :

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();
}

如果这不是更好的解决方案,请告知.但这似乎有效.

Please advise if this is not the better solution. But this seems to work.

这篇关于REST 服务返回错误的内容类型并解组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆