我目前正在实施一个Web API
>春天
>泽西岛
> com.thetransactioncompany.cors http://software.dzhuvinov.com/cors-filter.html
输出(如果有的话)将是JSON,因此我的所有类都使用预期的媒体类型进行注释.
@Produces(MediaType.APPLICATION_JSON)
public class CustomerResource {
...
}
这样我的类就会自动转换为json.
但…
由于微软,他们的IE只支持CORS,如果请求/响应类型是text / plain http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx
4. Only text/plain is supported for the request's Content-Type header
所以我需要强制我的应用程序在标题中使用text / plain进行响应,但仍然将我的类投影到json输出.我知道我添加的CORS类是设置该标题,但不知何故,我的注释会再次覆盖它,即使我自己添加了另一个过滤器.
因此,您只能接受纯文本,但可以随意生成您想要的内容.
编辑尝试使用类似的代码注册自定义responsefilter(也许你已经做过了吗?):
@Provider
public class HeaderRewriteFilter implements ContainerResponseFilter {
@Override
public ContainerResponse filter(ContainerRequest request,ContainerResponse response) {
response.setResponse(Response
.fromresponse(response.getResponse()).header(HttpHeaders.CONTENT_TYPE,"text/plain").build());
return response;
}
}
但是,检查结果以确保响应已包含此标头即可.
否则你可以尝试修改当前的响应,但我不确定你可以,因为它可能是一个不可变的对象.顺便说一句,它看起来不那么干净:)
List
另外,对于json<> java数据库,您可以查看Genson库http://code.google.com/p/genson/,它与Jersey完美集成.只需将jar放入类路径中即可运行!
编辑2确定然后你必须以另一种方式做,使用生成“text / plain”并为该类型定义一个json bodywriter.缺点是你只能生产json.使用Genson你可以这样做:
@Provider
@Produces({ MediaType.TEXT_PLAIN })
public class PlainTextJsonConverter extends GensonjsonConverter {
public GensonjsonConverter() {
super();
}
public GensonjsonConverter(@javax.ws.rs.core.Context Providers providers) {
super(providers);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。