1

I have a JSF managed bean and a JAX-RS web service.

ManagedBean - myBean class

@ManagedBean
@ViewScoped
public class MyBean implements Serializable {
     private String id;//getters && setters ommited
}

Web Service - myService class

@ApplicationPath("/myService")
@Path("/")
@Consumes("application/json")
public class MyService extends Application {

@Inject
MyBean myBean;

@Path("doSth")
@POST
public Response doSomething(){
    //make an action here and set myBean.id value 
    myBean.id = 'a random value';
}

}

I debugged the code above, but then I realised that inside myBean class, the property "id" was equal to null even though I had set it in the doSomething() method before, is it possible to achieve the scenario described above with these technologies? Before @Inject, I also tried the injection of my managedBean using @ManagedProperty, but the managedBean was null and not injected. Then I would like this myBean.id property to be changed for the bean when I call it using myBean.id, is that feasible?

Thanks in advance!

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
NickAth
  • 1,015
  • 1
  • 7
  • 22
  • Just make the jsf managed bean a cdi managed one... – Kukeltje Jun 30 '20 at 18:02
  • Hi Kukeltje, thanks for the answer, could you please provide me with some more details? – NickAth Jun 30 '20 at 18:16
  • Uhhhhh.... you already use cdi in the form of `@Inject` https://stackoverflow.com/questions/4347374/backing-beans-managedbean-or-cdi-beans-named#4347707 – Kukeltje Jun 30 '20 at 18:58
  • Yes I tried to replace the `@ManagedBean` annotation with `@Named` but I got many errors on the server (I am working currently on a mid-scale application, the code above is a sample) and I dropped this idea, I think I should spend some effort on this migration – NickAth Jun 30 '20 at 19:01
  • Does this answer your question? [Backing beans (@ManagedBean) or CDI Beans (@Named)?](https://stackoverflow.com/questions/4347374/backing-beans-managedbean-or-cdi-beans-named) – Kukeltje Jun 30 '20 at 19:01
  • Yes it seems to answer it even though I have not yet test it, thanks! – NickAth Jun 30 '20 at 19:08
  • But don't forget that `@Viewscoped` will not work in JAX-RS, even if you use the CDI counterpart . It is a jsf oriented scope so in a JAX-RS request it will always resolve to null. But besides that, and maybe I should write a full answer, if you (think you) need to inject a jsf oriented bean (a jsf 'controller' ) in a JAX-RS service, there is too much (business) logic in the JSF controller. Please read [JSF Controller, Service and DAO](https://stackoverflow.com/questions/30639785/jsf-controller-service-and-dao). Move the logic to a service and share that! – Kukeltje Jun 30 '20 at 21:07
  • This is indeed not the right approach. Take a step back and tell about your X-problem for which you incorrectly thought using a JAX-RS service to set a JSF view scoped bean property would be the correct solution. Only then we can give you the right answer. http://xyproblem.info – BalusC Jun 30 '20 at 22:47

0 Answers0