Jersey - Modify Query Parameter inside a Resource

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Jersey - Modify Query Parameter inside a Resource

cbou
I'm calling ResourceB Inside ResourceA.

Both uses the same query param "param1".

How can I change the value of "param1" inside ResourceA before calling ResourceB.

Here is the code:

package com.example;
 
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
import com.sun.jersey.api.core.ResourceContext;
 
public abstract class MyResource {
    ResponseBuilder response;
    Request request;
    protected String param1;
 
    public MyResource(@QueryParam("param1") String param1, @Context Request request) {
        this.param1 = param1;
        this.request = request;
        this.response = Response.ok();
    }
}
 
public class ResourceA extends MyResource {
    public ResourceA(String param1, Request request) {
        super(param1, request);
    }
    @Context private ResourceContext rc;
    public Response postJSON(String postData) {
        JSONObject data = JSONObject.fromObject(postData);
        if (data.has("resourceB")) {
            ResourceB resourceB = rc.getResource(ResourceB.class);
            // resourceB.setQueryParams("param1", "my new param 1");
            resourceB.postJSON(data.getJSONArray("resourceB"));
        }
    }
}
 
public class ResourceB extends MyResource {
    public ResourceB(String param1, Request request) {
        super(param1, request);
    }
 
    public Response postJSON(JSONArray data) {
        // this.params1 should not be "my new param 1"
    }
}