jnan77 / jsonrpc4j

Automatically exported from code.google.com/p/jsonrpc4j
0 stars 0 forks source link

Enhancement: support switch/property on JsonProxyFactoryBean to generate request with named parameters #29

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
It would be great to have a way to set the JsonProxyFactoryBean to generate 
JSON-RPC requests with named parameters (currently it always generates reqests 
with positional parameters). 

This would allow us to get around resolution issues with overloaded method like 
the one shown in the project documentation:

public void addFriend(UserObject userObject);
public void addFriend(UserObjectEx userObjectEx);

Proposal:
========
1. Annotate the interface method arguments with @JsonRpcParam annotation as

public void addFriend(@JsonRpcParam("user") UserObject userObject);
public void addFriend(@JsonRpcParam("userEx") UserObjectEx userObjectEx);

2. Introduce, then allow the ability to set the "namedParamsEnabled" property 
in JsonProxyFactoryBean to "true" on the client application

<bean id="exampleService" 
class="com.googlecode.jsonrpc4j.spring.JsonProxyFactoryBean">
  <property name="serviceUrl" value="http://remote.example.host:8080/myremoteapp/jsonRpc/exampleServiceRemote"/>
  <property name="serviceInterface" value="com.example.service.remote.ExampleService"/>
  <property name="namedParamsEnabled" value="true"/>
</bean>

3. Modify the invoke(MethodInvocation invocation) method in the 
JsonProxyFactoryBean.java class to something like:

    /**
     * {@inheritDoc}
     */
    public Object invoke(MethodInvocation invocation)
        throws Throwable {

        Object arguments = invocation.getArguments();

        // get return type
        Type retType = (invocation.getMethod().getGenericReturnType() != null)
            ? invocation.getMethod().getGenericReturnType()
            : invocation.getMethod().getReturnType();

        // prepare arguments as Map if namedParamsEnabled property is true 
        if (isNamedParamsEnabled()) {
            try{
                Map<String, Object> namedParams = new HashMap<String, Object>();
                Annotation[][] paramAnnotations = invocation.getMethod().getParameterAnnotations();
                for (int i=0; i<paramAnnotations.length; i++) {
                    Annotation[] ann = paramAnnotations[i];
                    boolean jsonRpcParamAnnotPresent = false;
                    for (Annotation an : ann) {
                        if (an instanceof JsonRpcParam) {
                            JsonRpcParam jAnn = (JsonRpcParam) an;
                            namedParams.put(jAnn.value(), invocation.getArguments()[i]);
                            jsonRpcParamAnnotPresent = true;
                            break;
                        }
                    }
                    if (!jsonRpcParamAnnotPresent) {
                        throw new RuntimeException();
                    }
                }
            arguments = namedParams;
            }catch(Exception e){
                // argument = invocation.getArguments();
            }
        }
        // invoke it
        return jsonRpcHttpClient.invoke(
            invocation.getMethod().getName(),
            arguments,
            retType, extraHttpHeaders);
    }

Original issue reported on code.google.com by austinon...@gmail.com on 20 Jul 2012 at 5:25

GoogleCodeExporter commented 8 years ago
I'll address this ASAP. (btw, a patch would have been great)

Original comment by brian.di...@gmail.com on 26 Jul 2012 at 5:36

GoogleCodeExporter commented 8 years ago
Applied and available in next release.

Original comment by brian.di...@gmail.com on 31 Jul 2012 at 7:09