JAX-RS:每个资源的选项 [英] JAX-RS: OPTIONS for every Resource
本文介绍了JAX-RS:每个资源的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将JAX-RS接口与XMLHttpRequest(XHR)一起使用.由于XHR预检,XHR在调用实际资源之前总是发送OPTIONS.
I am using a JAX-RS interface with XMLHttpRequest (XHR). Due to the XHR preflight, XHR send always OPTIONS before calling the real resource.
现在,我有数十种方法,每一次重新配置都需要OPTIONS.有什么办法可以自动执行此操作?我不想编写许多方法,例如:
Now I have dozens of methods and I need the OPTIONS for every resoruce. Is there any way to do this automatically? I dont want to write dozens of methods like:
@OPTIONS
@Path("/{id}")
@PermitAll
public Response optionsById() {
return Response.status(Response.Status.NO_CONTENT).build();
}
@OPTIONS
@Path("/{id}/data")
@PermitAll
public Response optionsByData() {
return Response.status(Response.Status.NO_CONTENT).build();
}
推荐答案
更新2013年9月12日:无效.使用所有这些@ GET/@ DELETE/@ POST/@ PUT不再起作用.
最后,我解决了我的问题.我创建了一个超类OptionsResource
,所有资源都从该超类继承.此资源包含:
Finally I solved my problem. I created a super class OptionsResource
, from which all resources inherit. This resoruce contains:
// Match root-resources
@OPTIONS
@PermitAll
public Response options() {
return Response.status(Response.Status.NO_CONTENT).build();
}
// Match sub-resources
@OPTIONS
@Path("{path:.*}")
@PermitAll
public Response optionsAll(@PathParam("path") String path) {
return Response.status(Response.Status.NO_CONTENT).build();
}
一个例子:
@Path("/test")
public class TestResource extends OptionsResource {
@GET
@Produces("text/plain;charset=UTF-8")
public Response index() {
return Response.status(Status.OK).entity("works").build();
}
}
此匹配项:
- curl -I -X选项 http://myhost.com/test
- 卷曲-I -X选项 http://myhost.com/test/asd/aasd /12/
- 等
- curl -I -X OPTIONS http://myhost.com/test
- curl -I -X OPTIONS http://myhost.com/test/asd/aasd/12/
- etc.
这篇关于JAX-RS:每个资源的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文