4

I added this method in my struts action,

public String execute() {
    long start = System.currentTimeMillis();
    simDetails = new SIMDetails();
    return GET_SIM_DETAILS;
}

and added below action in struts config file,

<result type="json" name="getSIMDetails">
    <param name="noCache">true</param>
    <param name="includeProperties">simDetails.*</param>
</result>

Then i got below JSON response

{
    "simDetails": {
        "void": null,
        "ban": null,
        "currentTariff": null,
        "currentTariffDescription": null,
        "defaultTariff": null,
        "defaultTariffDescription": null,
        "imsi": null,
        "packageItemId": null,
        "simSerialNumber": null,
        "simStatus": null,
        "simStatusCC": null,
        "status": null,
        "subscriberNumber": null,
        "subsidaryCode": null
    }
}

but I need this response instead of the above,

{
    "void": null,
    "ban": null,
    "currentTariff": null,
    "currentTariffDescription": null,
    "defaultTariff": null,
    "defaultTariffDescription": null,
    "imsi": null,
    "packageItemId": null,
    "simSerialNumber": null,
    "simStatus": null,
    "simStatusCC": null,
    "status": null,
    "subscriberNumber": null,
    "subsidaryCode": null
}

Any idea to get the required response with out add the above field to my action class.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Said Gamal
  • 65
  • 1
  • 1
  • 13

2 Answers2

3

You can use the root attribute as specified in the Root Object section of the documentation:

Use the "root" attribute(OGNL expression) to specify the root object to be serialized.

In your case:

<result type="json" name="getSIMDetails">
    <param name="noCache">true</param>
    <param name="root">simDetails</param>
</result>

P.S: this answer might be worthy of a read. And in the other answer to that question you can also see the Stream technique suggested by @IntelliData.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • If I used `dLink` it returns me **null**, and `` Works , and I dont want all attributes as response, why this is so, any suggestion, please – Shantaram Tupe Jun 21 '17 at 13:39
  • Sure ;) It's because your variable has a first word one-letter long, and Eclipse generates the getter `getdLink` instead of `getDLink`, as Struts2 needs. Just manually change the getter in `public String getDLink(){ return dLink;}` and it will work. Oh, and consider upvoting this answer, thanks @shantaram_t . – Andrea Ligios Jun 21 '17 at 13:48
  • ty Sir, This solved my problem but for my Ajax request, but another one doing such same thing not working now .. Oh I have upvoted your answer – Shantaram Tupe Jun 21 '17 at 14:03
  • The thing that now is not working was probably using the wrong getter/setter in a JSP, and you need to change them to the right syntax now that you've changed the getter and setter. BTW avoid variables with a first one-letter long word, for the future :) – Andrea Ligios Jun 21 '17 at 14:08
  • 1
    actually this is not my code I'm reusing it, I will use my variable then. – Shantaram Tupe Jun 21 '17 at 14:11
  • Wise choice @shantaram_t :) – Andrea Ligios Jun 21 '17 at 14:29
0

In order to avoid your very same problem, I usually return JSON using the following struts.xml (as opposed to JSON return type):

                <result name="success" type="stream">
                    <param name="contentType">text/html</param>
                    <param name="inputName">inputStream</param>
                </result>

I keep a variable 'inputStream' of type 'InputStream' in my action class, and in the execute() method, I manually assign the JSON to 'inputStream'. That allows me to customize the JSON exactly as I want it, and that is precisely what 'inputStream' will return.

Hope that helps!

IntelliData
  • 441
  • 6
  • 29