简体   繁体   English

如何在请求范围的 bean 中返回一个空主体

[英]How to return an empty body in request scoped bean

I have a request scoped bean which has a postconstruct method.我有一个请求范围的 bean,它有一个 postconstruct 方法。 In this method i call a seperate function in a seperate EJB.在这种方法中,我在单独的 EJB 中调用了单独的 function。 Based on the return value of the EJB i need to return an empty body with status code 200.根据 EJB 的返回值,我需要返回一个状态码为 200 的空主体。

For return http status code 200 you should create REST API endpoint and JSF bean must be you http client要返回 http 状态码 200,您应该创建 REST API 端点和 JSF bean 必须是您的 http 客户端

ExternalContext provides methods to set the required values for the response. ExternalContext提供了设置响应所需值的方法。 After you need to call FacesContext#responseComplete to signal JSF that the response has already been generated.在您需要调用FacesContext#responseComplete向 JSF 发出响应已经生成的信号之后。

@Named
@RequestScoped
public class YourBean {

    ....

    @Inject
    private FacesContext context;

    @Inject
    private YourEjb yourejb;

    public void someMethod() throws IOException {
        yourejb.someEjbMethod();

        ExternalContext ec = context.getExternalContext();
        ec.responseReset();
        ec.setResponseContentLength(0);
        ec.setResponseStatus(200);
        context.responseComplete();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM