1

First off my question is very similar to below however I'm not sure if the answers are applicable to my specific problem or whether I just need clarification about how to approach it: Convert LinkedHashMap<String,String> to an object in Java

I am using struts2 json rest plugin to convert a json array into a java array. The array is sent through an ajax post request and the java receives this data. However instead of being the object type I expect it is received as a LinkedHashmap. Which is identical to the json request in structure.

[
    {advance_Or_Premium=10000, available=true},
    {advance_Or_Premium=10000, available=true},
    {advance_Or_Premium=10000, available=true}
]

The data is all present and correct but just in the wrong type. Ideally I want to send the data in my object type or if this is not possible convert the LinkedHashMap from a list of keys and values into the object array. Here is the class I am using, incoming data is received in the create() method:

@Namespace(value = "/rest")
public class OptionRequestAction extends MadeAbstractAction implements ModelDriven<ArrayList<OptionRequestRest>>
{
  private String id;

  ArrayList<OptionRequestRest> model = new ArrayList<OptionRequestRest>();

  public HttpHeaders create()
  {
    // TODO - need to use model here but it's a LinkedHashmap
    return new DefaultHttpHeaders("create");
  }

  public String getId()
  {
    return this.id;
  }

  public ArrayList<OptionRequestRest> getModel()
  {
    return this.model;
  }

  public ArrayList<OptionRequestRest> getOptionRequests()
  {
    @SuppressWarnings("unchecked")
    ArrayList<OptionRequestRest> lReturn = (ArrayList<OptionRequestRest>) this.getSession().get("optionRequest");

    return lReturn;
  }

  // Handles /option-request GET requests
  public HttpHeaders index()
  {
    this.model = this.getOptionRequests();
    return new DefaultHttpHeaders("index").lastModified(new Date());

  }

  public void setId(String pId)
  {
    this.id = pId;
  }

  public void setModel(ArrayList<OptionRequestRest> pModel)
  {
    this.model = pModel;
  }

  // Handles /option-request/{id} GET requests
  public HttpHeaders show()
  {
    this.model = this.getOptionRequests();
    return new DefaultHttpHeaders("show").lastModified(new Date());
  }
}

One of the things which is confusing me is that this code works fine and returns the correct object type if the model is not an array. Please let me know if my question is not clear enough and needs additional information. Thanks.

11
  • 1
    Please post JSON that is sent to the action. Commented May 7, 2015 at 11:54
  • 1
    The full request is pretty big, I'll post a snippet. Edit - Json is not valid, I'll have a play with that quickly before posting as this might be the issue. Commented May 7, 2015 at 12:12
  • 1
    Try 1) to change all your ArrayList declarations to the interface List, and if nothing changes try 2) removing ModelDriven Commented May 7, 2015 at 14:17
  • 1
    No I mean in the Action: instead of ArrayList<OptionRequestRest> model = new ArrayList<OptionRequestRest>();, use List<OptionRequestRest> model = new ArrayList<OptionRequestRest>();, and so in every other place (getters, setters, model etc) Commented May 7, 2015 at 14:35
  • 1
    First try was due to this, while the try without ModelDriven was due to the many weird problems ModelDriven creates, especially with the interceptor position in the stack (even in the default one!). BTW the question is not clear, stop using the word Array when referring to a Collection, consider posting an SSCCE, and eventually take a look at the following links Commented May 8, 2015 at 10:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.