Categories: java, spring, spring-mvc, spring5

Objects Values Not Persisting. (Spring MVC)

1 answer

I ma trying to migrate some old code from WebSphere to Tomcat. The older code used Spring 3.2, now I upgraded the JARs to 5.2.2. But somehow object values just do not persist.

My controller class is:

@Controller @Scope("session") public class OperationController {  private GUIDataObject guiDO = null;    /**  * Constructor  */ public OperationController() {  }  @RequestMapping(value="/readDataSource") @ResponseBody public String readDataSource() {      try {          String[] sources = guiDO.getDataSources();          .         .         .         Code to work on Array sources         .         .         .          return "ok";      } catch (Exception e) {          return "Error: " + e.getMessage();     }        }   /**  * Set the data sources in the Data Storage Area - these are passed as a "parameter" map  * in the request.  * @param webRequest : WebRequest which parameter map can be pulled from  * @return "ok"  */     @RequestMapping(value="/setDataSources") @ResponseBody public String setDataSources(WebRequest webRequest) {      guiDO.setDatasources(webRequest.getParameterMap());      return "ok"; }  . . . Lots of other code. . . .  } 

and the values are stored in the object:

public class GUIDataObject {  private String service; private String uniqueProcessId; private String userId; private String vendor;  // record the data sources to read from private Map<String, String[]> dataSources = null;  public GUIDataObject(String service, String uniqueProcessId, String userId, String vendor) {      super();      this.service = service;     this.uniqueProcessId = uniqueProcessId;     this.userId = userId;     this.vendor = vendor; }   public void setDatasources(Map<String, String[]> datasources) {     this.dataSources = datasources; }   public String[] getDataSources() throws Exception {      if (this.dataSources == null) {         throw new Exception("No datasources have been set from the GUI");     }      if (!this.dataSources.containsKey("source")) {         throw new Exception("No datasources have been set from the GUI");     }      return this.dataSources.get("source"); }  . . . Lots of methods. . . . } 

Now my problem is the dataSources Map is getting set fine. But when fetching the values they return empty. It errors out in the second if-block, so I can say at least its not null. There are other Maps/Strings in the object as well, but I cant really tell if they are being properly set or not, since, this is the first method that is being hit and it errors out after that. I can see that the values that are initialized in the constructor are being retained just fine. So cant really where it is going wrong.

The same code worked fine on WebSphere and Spring 3.2. Now I am not sure if there are any new configurations that I need to in order to get this to work. Since 3.2 is very very old. Any help on this would be appreciated.

All answers to this question, which has the identifier 61222761

The best answer:

The problem was the way webRequest.getParameterMap() works in WebSphere and Tomcat. In WebSphere it returns a concrete HashTable. But in Tomcat, it returns a org.apache.catalina.util.ParameterMap which is a subclass of HashMap. And somehow they just don't mix. Even casting throws a ClassCastException.

I got it to work by changing the dataSources to a HashMap.

private HashMap<String, String[]> dataSources = null; 

and the set method to:

public void setDatasources(Map<String, String[]> datasources) {      if (this.dataSources == null) {          this.dataSources = new HashMap<String, String[]>();          this.dataSources.putAll(datasources);      } else {          this.dataSources.putAll(datasources);     } 

Probably I could have left the dataSources as a Map and it still would have worked. But I didn't try it out.

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data