Wracker
Wracker

Reputation: 619

Mapping all request parameters into a class object

Long story short, I'm trying to map all the parameters that are being sent as part of the request to a custom class.

custom.class

public class data
{
    private String a;
    private String b;

    public String GetA()
    {
        return this.a;
    }

    public String SetA(String a)
    {
        this.a = a;
    }

    public String GetB()
    {
        return this.a;
    }

    public String SetB(String b)
    {
        this.b = b;
    }
}

now I can access the parameters like this:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    request.getParameter("a");
    request.getParameter("b");
}

But as I mentioned above , I would like all the parameters to be loaded to the class object.

I think the best way would be converting the parameters into a map object and then somehow evaluate the class object with all the values we retrieve from the map object. However I have no idea how to make this happen.

So I'd really appreciate if someone could point me to a documentation or advise me as how could this be done?

Apologies for my bad english.

Thanks in advance, Alex

Upvotes: 1

Views: 5624

Answers (3)

Benedikt Schmidt
Benedikt Schmidt

Reputation: 2278

I know this is a bit old, but I stumbled upon a similar problem for which I came up with a rather simple solution.

My approach is to use the google gson implementation to get a Json object with simple string properties, which can then be parsed into the object of your choice (again with gson). It maps the key-value pairs of the created Json object into your object if existing. So it actually is pretty versatile and can be adjusted to any kind of object, without self made attribute-to-attribute-mapping.

Let's just assume you have a target object, where you want to wrap your request parameters, that looks something like this:

public class RequestInfo {
    private String a;
    private String b;

    public RequestInfo() {
    }

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }
}

Your HTTPServletRequest has this fancy parameter map that contains a key and a value. The value is a String array, I can't really tell you why, but in my case I just assume every property that I have results in only one possible value, which is why I only use the first entry of it. The idea is simple: Create an empty Json object, iterate over the parameter map and create key-value-pairs inside your Json object. Let's take a look:

// create an empty json object
JsonObject requestJson = new JsonObject();
// iterate over all the paramters
for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
    // add every item as a property to the json
    requestJson.addProperty(entry.getKey(), entry.getValue()[0]);
}

Now you have a really simple Json Object with all your request parameters mapped as key-value-pairs of type String. Finally, just let gson parse this object into your own representation. One line of code, nothing more:

// let Gson parse the Json to your own object
RequestInfo info = new Gson().fromJson(requestJson, RequestInfo.class);

And now you have all matching parameters parsed into your own object. Note that Gson is also able to recognize different Object types and parse them correctly. So even though we only create String key-value-pairs in the Json object, gson is able to parse them into Integers, Longs etc. if you specified your object like that.

Now if your request info object changes, you don't have to update any matching algorithm with additional cases, if-else structures or updated constructors, gson will handle it for you. And, if you have different types of requests that you want to map, you can pretty much use the same algorithm, you just have to update the class definition in the fromJson-call.

Hope this is helpful.

Upvotes: 1

Mohamed Elbeialy
Mohamed Elbeialy

Reputation: 31

If i understand you well , You should do a constructor in your data class that take a, b as parameters:

public data(String aa, String bb){
    a = aa;
    b = bb;
}

Then you can call your constructor in your doPost() method:

data d = new data(request.getParameter("a"), request.getParameter("b"));

Upvotes: 0

Predrag Maric
Predrag Maric

Reputation: 24423

There already is a method on HttpRequest for getting parameters as a map, is this something you could use? You could iterate through the parameter map, and fill your custom class for each entry

    YourDataClass data = new YourDataClass();
    Map<String, String[]> map = request.getParameterMap();
    for (Map.Entry<String, String[]> entry : map.entrySet()) {
        // do whatever you need
        data.addParam(entry.getKey(), entry.getValue());
    }

Upvotes: 1

Related Questions