calling a method with parameters

66 views
Skip to first unread message

Kand

unread,
Apr 13, 2011, 4:57:12 AM4/13/11
to jpoxy
Hi all,

Say if I have a method getString(String s) exposed through jpoxy.

Can anyone tell me how to call this method.

Regards,
Kand

Wes Widner

unread,
Apr 13, 2011, 7:25:58 AM4/13/11
to jp...@googlegroups.com
If your servlet is configured to listen on / you can call your method via the following:

/?method=getString&params["string s"]
/?json={"method":"getString","params":["string s"]}
/?json={"method":"getString","params":{"notused":"string s"}}

All of these end up doing the same thing. I recommend using urlrewrite to simplify your common commands. For example, with urlrewrite you can make /news/1 redirect to ?method=getNews&id=1

The above examples are all GET requests, but you can use POST in the same way if you want.

-Wes

Kand

unread,
Apr 13, 2011, 8:34:00 AM4/13/11
to jpoxy
Hi Wess,
When i try to call the method i am getting the following error
{"error":{"message":"JSON-RPC method [getString] with 0 parameters not
found.","data":{"classname":"org.jpoxy.JSONRPCException","hashcode":
1555298,"stacktrace":["org.jpoxy.RPC.handleRequest(RPC.java:
709)","org.jpoxy.RPC.doGet(RPC.java:
505)","javax.servlet.http.HttpServlet.service(HttpServlet.java:
690)","javax.servlet.http.HttpServlet.service(HttpServlet.java:
803)","org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
290)","org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:
206)","org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:
96)","org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
235)"

The url i am using to call the method is
http://<Ip>:8080/JPoxyRPC/example?method=getString&params["hello"]

Any idea where the problem is?

Regards,
Kandasamy

On Apr 13, 4:25 pm, Wes Widner <kai5263...@gmail.com> wrote:
> If your servlet is configured to listen on / you can call your method via
> the following:
>
> /?method=getString&params["string s"]
> /?json={"method":"getString","params":["string s"]}
> /?json={"method":"getString","params":{"notused":"string s"}}
>
> All of these end up doing the same thing. I recommend using
> urlrewrite<http://www.tuckey.org/urlrewrite/>to simplify your common
> commands. For example, with urlrewrite you can make
> /news/1 redirect to ?method=getNews&id=1
>
> The above examples are all GET requests, but you can use POST in the same
> way if you want.
>
> -Wes
>
>
>
> On Wed, Apr 13, 2011 at 4:57 AM, Kand <kans...@gmail.com> wrote:
> > Hi all,
>
> > Say if I have a method getString(String s) exposed through jpoxy.
>
> > Can anyone tell me how to call this method.
>
> > Regards,
> > Kand- Hide quoted text -
>
> - Show quoted text -

Wes Widner

unread,
Apr 13, 2011, 9:10:04 AM4/13/11
to jp...@googlegroups.com
It looks like you have a syntax error in the params parameter. Your query should look like:

http://<Ip>:8080/JPoxyRPC/example?method=getString&params=["hello"]

-Wes

Kand

unread,
Apr 14, 2011, 2:03:05 AM4/14/11
to jpoxy
Hi Wes,

Thanks for you help.

I have one more doubt though.

I have one spring xml in the war I created for jpoxy.

Can you tell me how to access the beans inside the method i expose
through jpoxy.

Would be great if you can guide me here.

Regards,
Kand

On Apr 13, 6:10 pm, Wes Widner <kai5263...@gmail.com> wrote:
> It looks like you have a syntax error in the params parameter. Your query
> should look like:
>
> http://<Ip>:8080/JPoxyRPC/example?method=getString&params=["hello"]
>
> -Wes
>
> On Wed, Apr 13, 2011 at 7:25 AM, Wes Widner <kai5263...@gmail.com> wrote:
> > If your servlet is configured to listen on / you can call your method via
> > the following:
>
> > /?method=getString&params["string s"]
> > /?json={"method":"getString","params":["string s"]}
> > /?json={"method":"getString","params":{"notused":"string s"}}
>
> > All of these end up doing the same thing. I recommend using urlrewrite<http://www.tuckey.org/urlrewrite/>to simplify your common commands. For example, with urlrewrite you can make
> > /news/1 redirect to ?method=getNews&id=1
>
> > The above examples are all GET requests, but you can use POST in the same
> > way if you want.
>
> > -Wes
>

Kand

unread,
Apr 14, 2011, 2:15:43 AM4/14/11
to jpoxy
Hi Wes,

Basically I want to know how to get servletcontext in jpoxy so that i
can use WebApplicationContext to get Beans.

Regards,
Kand

Wes Widner

unread,
Apr 14, 2011, 9:51:45 AM4/14/11
to jp...@googlegroups.com
I added that functionality yesterday in the 1.0.16 release.

What you'll need to do is implement the JSONRPCListener interface and listen for the init event. Here is an example of getting a reference to the servlet's context.

public class MyRPCClass implements JSONRPCEventListener {

    protected ServletConfig config;
    protected ServletContext context;
    
    @JpoxyIgnore
    public void messageReceived(JSONRPCMessageEvent me) {
        switch (me.message().getCode()) {
            case JSONRPCMessage.INIT:
                config = me.message().getServletConfig();
                context = me.message().getServletcontext();
                break;
        }
    }
}

From here you can reference either the servlet config or context in your methods when they are called.

-Wes

Zircon

unread,
Apr 15, 2011, 12:35:42 PM4/15/11
to jpoxy
Sadly, the way jpoxy is implemented doesn't allow it to work with Dojo
SMDs because DOJO can't send the parameters. I would really like to be
able to pass params like this:

for String testMethod(String param1, String param2) {...}

http://<Ip>:8080/JPoxyRPC/example?method=testMethod&params_param1="value1"&params_param2="value2"

or something else instead of params_


On Apr 13, 2:25 pm, Wes Widner <kai5263...@gmail.com> wrote:
> If your servlet is configured to listen on / you can call your method via
> the following:
>
> /?method=getString&params["string s"]
> /?json={"method":"getString","params":["string s"]}
> /?json={"method":"getString","params":{"notused":"string s"}}
>
> All of these end up doing the same thing. I recommend using
> urlrewrite<http://www.tuckey.org/urlrewrite/>to simplify your common

Wes Widner

unread,
Apr 15, 2011, 1:53:55 PM4/15/11
to jp...@googlegroups.com
We should probabally add the ability to match url parameters to methods like we currently do for the params object of the json or data parameter. However you can still access the url parameters by accepting a HashMap in your method.

For example:

String testMethod(HashMap params) {
           String param1 = params.get("param1");
           String param2 = params.get("param2");
}

As a bonus, you also recieve a copy of the HttpServletRequest, which also makes session access possible.

Here is a snippet I use all the time:
HttpServletRequest request = (HttpServletRequest) params.get("request");
HttpSession session = request.getSession(true);

-Wes

Zircon

unread,
Apr 15, 2011, 4:20:13 PM4/15/11
to jpoxy
I know how it currently works, I took a look at the sources :)
Considering I can't build the damn params map from Dojo and it's
easier for me to write java than javascript, I will build a parameter
system like the one i described. Will post it here after it's done.


On Apr 15, 8:53 pm, Wes Widner <kai5263...@gmail.com> wrote:
> We should probabally add the ability to match url parameters to methods like
> we currently do for the params object of the json or data parameter. However
> you can still access the url parameters by accepting a HashMap in your
> method.
>
> For example:
>
> String testMethod(HashMap params) {
>            String param1 = params.get("param1");
>            String param2 = params.get("param2");
>
> }
>
> As a bonus, you also recieve a copy of the HttpServletRequest, which also
> makes session access possible.
>
> Here is a snippet I use all the time:
>
> HttpServletRequest request = (HttpServletRequest) params.get("request");
> HttpSession session = request.getSession(true);
>
> -Wes
>
>
>
> On Fri, Apr 15, 2011 at 12:35 PM, Zircon <alex.bar...@gmail.com> wrote:
> > Sadly, the way jpoxy is implemented doesn't allow it to work with Dojo
> > SMDs because DOJO can't send the parameters. I would really like to be
> > able to pass params like this:
>
> > for String testMethod(String param1, String param2) {...}
>
> > http://
> > <Ip>:8080/JPoxyRPC/example?method=testMethod&params_param1="value1"&params_­param2="value2"

Kand

unread,
Apr 18, 2011, 8:50:54 AM4/18/11
to jpoxy
Hi Wes,

Can you tell me how to pass json object created by jackson in the
jpoxy url in android

I have a getProduct(String val) method which is exposed through jpoxy.

The following is the way i am trying to access the method from android


Product p= new Product("apple",20);
ObjectMapper mapper = new ObjectMapper();

String str= mapper.writeValueAsString(p);
String str1="\""+str+"\"";

System.out.println("str1" + str1);
JSONRPCClient client = JSONRPCClient.
create("<ip>:8080/Jpox/Example?method=getProducts&params=["+str1+"]");

I am getting the below exception.
ERROR/AndroidRuntime(396): Caused by:
java.lang.IllegalArgumentException: Illegal character in query at
index 66:
http://<ip>:8080/Jpoxy/Example?method=getProducts&params=["{"name":"apple","obj":null,"price":20}"]

Can you tell me where is the problem.

Regards,
Kand

Wes Widner

unread,
Apr 18, 2011, 8:59:47 AM4/18/11
to jp...@googlegroups.com
It looks like your quotes inside the params parameter are wrong. You should change the outer quotes to single quotes, ie.
http://<ip>:8080/Jpoxy/Example?method=getProducts&params=['{"name":"apple","obj":null,"price":20}']

You might also want to urlencode the params parameter to keep the java net library from throwing an exception.

Another way to accomplish what you are trying to do, which seems to be passing a json object, is to have your method accept a JSONObject, ie. getProduct(JSONObject prod) Then you could call it with
http://<ip>:8080/Jpoxy/Example?method=getProducts&params={"name":"apple","obj":null,"price":20}

And you would get the json object already decoded into a standard JSONObject where you could use it with prod.getInt("price");

-Wes
Reply all
Reply to author
Forward
0 new messages