I'm using Payara Microservice for a Java web application and want to inject a bean into my deserialised JSON object so that I can add perform some additional logic.
So far I have the resource class, annotated with @Path and @POST, which I call via Postman with some JSON. The method invokes fine, however no matter what I try, I am unable to get the bean to inject.
The JSON object's class that looks like this:
public class IncomingJsonRequest {
@NotNull
private String value;
private AdditionalLogicClass additionalLogicBean;
public String setValue(String value) {
this.value = value;
}
public void performAdditionalLogic() {
additionalLogicBean.performLogic(value);
}
}
What I would like to do is inject the AdditionalLogicClass
bean so that when I call the performAdditionalLogic()
method from the resource method it doesn't throw a null pointer exception.
I've tried all sorts of annotations and so far the only way I can seem to do this is for the resource class to pass the bean in, but that's not good encapsulation. I don't want the resource to know about how this additional logic is done.
The other way was programatically loading the bean but I've read that it's not good practice.
What is the best way to achieve this?
Thanks