简体   繁体   中英

Object Serialization Issue With Wicket

I am trying to pass a searchService into a Form into Wicket to use. When I run this with out the searchService being final I get no issues but I have to make it final in order to use it inside the overwritten onSubmit.

    final SearchService searchService = new SearchService();

    Form<?> form = new Form("searchForm") {
        public void onSubmit() {
            List<File> results = searchService.getSearchResults();
            info("Found: " + results.size());
        }
    };

If I do that I receive an error:

org.apache.wicket.core.util.objects.checker.CheckingObjectOutputStream$ObjectCheckException: The object type is not Serializable!
A problem occurred while checking object with type: test.service.SearchService
Field hierarchy is:
  2 [class=test.webui.HomePage, path=2]
    private java.lang.Object org.apache.wicket.MarkupContainer.children [class=[Ljava.lang.Object;]
      private java.lang.Object org.apache.wicket.MarkupContainer.children[1] [class=org.apache.wicket.markup.html.form.Form, path=2:searchForm]
        private final test.service.SearchService test.webui.HomePage$1.val$searchService [class=test.service.SearchService] <----- field that is causing the problem

I'm new to wicket so I'm wondering if there is a correct way to do this?

Yes. It is expected behavior.

Wicket will serialize the whole page. In fact, the whole pageflow.

The error you are seeing is great because it helps you make your application work in a replicated session cluster.

To solve it you have several options. Off the top of my head:

  • You could instantiate the SearchService inside the onSubmit form instead of passing it as an attribute.
  • You could use some sort of dependency injection. Wicket supports Guice and Spring out of the box. But nothing stops you from using the injector proxies with other dependency injectors.
  • You could store the SearchService on the Wicket Application instead of the page. The wicket application is a non-serializable singleton per cluster node, and it is available in the thread.

You can try to implement Serializable to your SearchService class and declare a variable serialVersionUID .

Eg:

public class SearchService implements Serializable {
    private static final long serialVersionUID = -1769512858853931584L;
    //your code
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM