I'm trying to use a Json View for Spring (http://spring-json.sourceforge.net/) (org.springframework.web.servlet.view.json.JsonView) but whenever I write a controller class that extends AbstractController
I get the following Error:
java.lang.IllegalStateException: ApplicationObjectSupport ins开发者_Go百科tance [org.springframework.web.servlet.view.json.JsonView] does not run in an ApplicationContext
The weird thing is, that when I implement the Controller interface directly and do not inherit, it is fine. The error only happens when I inherit from AbstractController
.
In my current case though I would like to extend AbstractFormController
and hence can't write a class that does not inherit from AbstractController
.
Any ideas?
That's a rather misleading error message, it's actually complaining that the JsonView is not running inside an app context. What it means is that the JsonView
bean was not instantiated by Spring, but that you instantiated it yourself (JsonView
extends ApplicationObjectSupport
, and should therefore be Spring-managed).
However, you haven't given us any of your code, so it's hard to tell for sure. I'm guessing your controller is instantiating JsonView
itself? You need to let Spring do that, either by injecting a JsonView
bean into the controller, or perhaps using a ViewResolver
(if Spring-Json supplies one).
If you are doing Java configuration ( as opposed to XML ), in your configuration class you may want to call setApplicationContext
method on object that is complaining.
This is what did the trick for me in Spring MVC 3.2.2 when trying to initialize ContentNegotiatingViewResolver
in Java.
Here is the sample configuration class:
@Configuration
@EnableWebMvc
...
public class MyConfig
{
@Inject
private ApplicationContext appContext;
@Bean
public ContentNegotiatingViewResolver contentNegotiatingViewResolver ( )
{
ContentNegotiatingViewResolver retVal =
new ContentNegotiatingViewResolver( );
...
retVal.setApplicationContext( appContext );
return retVal;
}
}
精彩评论