0

I have web services developed using Jersey, and a service take as parameter a Java Bean annotated with @QueryParam annotations on fields. It works fine when the service is invoked directly via its URL. Now I wish to call that service programatically from another piece of code (a JSP in the same WAR, say). I wish to have the bean parameter filled in with the query parameters of my current request, basically doing myself what Jersey does for me automatically when I call the service URL.

I want really to be able to take the request parameters and inject them into the relevant bean fields. I know I could do that myself with BeanUtils and reading the annotations myself, but surely there is an easier way?

Example code: My service defines this method

@GET
public Response generate(@BeanParam Options options){...}

And Options is a Bean that has fields like

@QueryParam("format") 
private String format="pdf";

I want to be able to write something like:

Options myoptions=new Options();
???.inject(myoptions,request);

in my JSP.

Does it make sense?

JP Moresmau
  • 7,388
  • 17
  • 31
  • I'm not quite sure what exactly this has to do with JAX-RS or Jersey at all if this is something you are trying to implement in the JSP. Are you using Jersey's MVC feature or something? If not, maybe something you might want to [look into](https://jersey.java.net/documentation/latest/mvc.html) – Paul Samsotha Jan 04 '16 at 13:25
  • Well my bean is annotated with JAX-RS annotations like QueryParam, and I have a HttpServletRequest I want to use to populate the annotated fields, so it has everything to do with JAX-RS.. I just want to be able to use my service via a direct API call and not only via HTTP. – JP Moresmau Jan 04 '16 at 13:34
  • But how do you figure JSP fits in with JAX-RS? That's what I don't get. How do you expect these two things to interact? – Paul Samsotha Jan 04 '16 at 13:42
  • @peeskillet Jersey MVC templates - https://jersey.java.net/documentation/latest/mvc.html . But I don't have the idea that is what is being used here. – Gimby Jan 04 '16 at 13:46
  • @Gimby I already suggested that, but I am not sure if that is what the OP is looking for. – Paul Samsotha Jan 04 '16 at 13:48
  • As I see it, web services are a way to expose some code. But since the services and the parameters are also Java objects, I can use the service code as normal Java code if I run code in the same servlet container. So here I have services that are exposed via HTTP as normal, but I also bundle a JSP based UI that can access the services directly. This works fine, I just want to simplify the building of parameters from the request I get to the JSP. I suppose I could call the service with the MVS approach too, thanks for the hint! – JP Moresmau Jan 04 '16 at 14:15
  • What the MVC does is allow Jersey to serve the JSP rather than the servlet container. So Jersey would be the one processing the JSP templates. You can see an example [here](http://stackoverflow.com/a/31900846/2587435). – Paul Samsotha Jan 04 '16 at 14:31

0 Answers0