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.

Community
  • 1
  • 1
Suipaste
  • 420
  • 7
  • 17
  • 1
    Please post JSON that is sent to the action. – Roman C May 07 '15 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. – Suipaste May 07 '15 at 12:12
  • You can create custom ContentTypeHandler. – Aleksandr M May 07 '15 at 12:35
  • The json is valid - what I was copying from was just an incomplete preview. Because the actual json request and it's object structure is massive I can't really post it. However it's structure is the same as the (simplified) LinkedHashmap which I posted above, so for all intents and purposes looks exactly the same except it's a string. – Suipaste May 07 '15 at 13:07
  • I'm guessing a ContentTypeHandler is something which can be used to convert a linkedHashmap. Ideally though I would like to use tools which are part of the plugin to do this nativity rather than bloating my code with unnecessary converters. – Suipaste May 07 '15 at 13:14
  • 1
    Try 1) to change all your ArrayList declarations to the interface List, and if nothing changes try 2) removing ModelDriven – Andrea Ligios May 07 '15 at 14:17
  • @Andrea Ligios see the edit at the bottom of my question - is this what you mean? If so I have already tried it but I suspect you mean something else. – Suipaste May 07 '15 at 14:33
  • 1
    No I mean in the Action: instead of `ArrayList model = new ArrayList();`, use `List model = new ArrayList();`, and so in every other place (getters, setters, model etc) – Andrea Ligios May 07 '15 at 14:35
  • Tried changing the ArrayList declarations to List. The rest interface behaved exactly the same as far as I could tell. Also tried removing modelDriven which broke the rest interface - I can't understand how this could help. – Suipaste May 07 '15 at 15:13
  • 1
    First try was [due to this](http://stackoverflow.com/a/15108654/1654265), while the try without ModelDriven was due to the [many weird problems ModelDriven creates](http://stackoverflow.com/a/21021131/1654265), 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](http://sscce.org/), and eventually take a look at the following links – Andrea Ligios May 08 '15 at 10:08
  • 1
    1) http://struts.apache.org/docs/type-conversion.html 2) struts.apache.org/docs/annotations.html#Annotations-TypeConversionAnnotations 3) http://struts.apache.org/docs/typeconversion-annotation.html – Andrea Ligios May 08 '15 at 10:08

0 Answers0