0

I understand the problem. List is an interface an Spring does not know what to instantiate. One solution is to let my controller(and services) return an ArrayList. I really would like to avoid this. How can this be done instead?

SEVERE: Servlet.service() for servlet [Spring MVC Dispatcher Servlet] in context with path threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface] with root cause
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface


public @ResponseBody
    List<Preference> getPreferences(@PathVariable Long userId, List<Preference> preferences, ModelMap data) {
pethel
  • 5,397
  • 12
  • 55
  • 86
  • 1
    I'd rather say that exception complains about `List preferences` argument, because it's not annotated hence probably Spring MVC cannot resolve it (unless you have your own `WebArgumentResolver` registered). – Grzegorz Rożniecki Sep 28 '12 at 14:01
  • aha ok you are rigth. Must I use @RequestBody? Other possiblites? – pethel Sep 28 '12 at 15:11
  • `@RequestBody` is only possible with AJAX request, i.e. when you send list of `Preference` objects as JSON data. `@RequestParam` is another option, but 1) [it does accept only arrays, not Lists](http://stackoverflow.com/questions/4596351/binding-a-list-in-requestparam) 2) you should have StringToPreferences [type converter registered](http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-typeconversion) in `initBinder`. – Grzegorz Rożniecki Sep 28 '12 at 15:34

1 Answers1

1

Before you try to solve your problem, think what kind of solution do you expect? You want to marshal List of some objects into JSON. How should this JSON look like? If you were to write test first, what would you put in your assertions?

Hint: this is not a valid JSON:

[1, 2, 3, 4, 5]

neither is this:

[{a: 1}, {b: 2}]

To cut the long story short: you need an object wrapping list, both to marshal as JSON and as XML:

{array: [1, 2, 3, 4, 5]}

That being said, just wrap your list in some simple object:

class Wrapper {

    private List<Preference> preferences;

}

public @ResponseBody Wrapper getPreferences(/*...*/)

Note that the Wrapper class name is discarded, but preferences field name will be used.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • 1
    Your first JSON example *is* valid JSON - according to http://jsonlint.com/. Thus a simple List is sufficient after all. – nickdos Sep 28 '12 at 22:21
  • @nickdos: thanks. According to http://json.org it's **not** a valid JSON (?), I'm a bit confused, will have to check that later. – Tomasz Nurkiewicz Sep 28 '12 at 22:32
  • Also, the "valid JSON" link you pasted, says JSON can be "An ordered list of values. In most languages, this is realized as an array" and "An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma)". – nickdos Sep 28 '12 at 22:55
  • @nickdos: OK, just to be on the safe side, I am removing the first example. Thanks for clarification. – Tomasz Nurkiewicz Sep 29 '12 at 10:54