JAXRS响应通用标头

发布于 2025-01-20 21:47:11 字数 1308 浏览 5 评论 0原文

有什么方法可以将通用标题设置为JAXR中所有API路径的响应吗?
例如,我有这样的API:

@Path("/api/v1")
public class JaxRsConfig extends Application {
}

而且

@Path("/voucher")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class Voucher {
    
    @POST
    public Response add(...) {
        return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
    }

    @GET
    public Response get(...) {
        return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
    }

    @GET
    public Response list(...) {
        return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
    }
} 

@Path("/invoice")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class Invoice {

    @POST
    public Response add(...) {
        return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
    }

    @GET
    public Response get(...) {
        return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
    }

    @GET
    public Response list(...) {
        return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
    }
}   

我总是必须将此标头放在响应中。
JAXRS通常可以设置此标头的机制?
注意:我在Liberty应用程序服务器上使用Javaee-8

is there any way to set general header on response of all api paths in JaxRS ?
for example i have a api like this :

@Path("/api/v1")
public class JaxRsConfig extends Application {
}

and

@Path("/voucher")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class Voucher {
    
    @POST
    public Response add(...) {
        return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
    }

    @GET
    public Response get(...) {
        return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
    }

    @GET
    public Response list(...) {
        return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
    }
} 

and this:

@Path("/invoice")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class Invoice {

    @POST
    public Response add(...) {
        return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
    }

    @GET
    public Response get(...) {
        return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
    }

    @GET
    public Response list(...) {
        return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
    }
}   

I always have to put this header in the response .
JaxRs has any mechanism to set this header generally ?
Note: I use JavaEE-8 on Liberty Application Server

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开雨落又逢春i 2025-01-27 21:47:11

您可以尝试使用 WriterInterceptor 来添加此标头。
这里解释了一些很好的例子: https://dennis-xlc.gitbooks.io/restful-java-with-jax-rs-2-0-2rd-edition/content/en/part1/chapter12/reader_and_writer_interceptors.html

You can try to use a WriterInterceptor to add this header.
Some good examples explained here: https://dennis-xlc.gitbooks.io/restful-java-with-jax-rs-2-0-2rd-edition/content/en/part1/chapter12/reader_and_writer_interceptors.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文