Ho provato ad avere un controller in Spring restituisce una risposta JSON senza alcun risultato utilizzando le classi Jackson come recommended with 3.0. Ho i file jar di jackson (jackson-core-asl-1.5.5.jar & jackson-mapper-asl-1.5.5.jar) ovviamente nel mio percorso di classe.Spring Json non risolto con risposta appropriata
Per quanto riguarda le voci appconfig.xml, non sono sicuro di aver bisogno di queste. Li ho messi lì come ultimo atto di disperazione prima di tornare alla vecchia moda non-json.
In di debug, guardo il regolatore di ottenere la richiesta, restituire il foo e poi, in Firebug, ottengo un 406.
I messaggi di errore sono i seguenti: dal registratore quando è impostato per eseguire il debug: org .springframework.web.HttpMediaTypeNotAcceptableException: Impossibile trovare rappresentazione accettabile
dalla risposta: (406) la risorsa identificata da questa richiesta è solo in grado di generare risposte con caratteristiche non accettabili in base alla richiesta di "accettare" le intestazioni() .
mio appconfig.xml è qui:
<!-- Configures support for @Controllers -->
<mvc:annotation-driven />
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</list>
</property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"></property>
</bean>
mio regolatore
@RequestMapping(value="foo/bar", method=RequestMethod.GET)
public @ResponseBody foo getFoo(@RequestParam String fooId) {
return new foo(fooId);
}
Sul jsp, in cui viene effettuata la chiamata AJAX:
function addRow() {
$.getJSON("foo/bar",{ fooId: 1} , function(data) {
alert("it worked.");
});
}
Fammi sapere se c'è un ny più informazioni necessarie.
più copertura a [Spring JSON request getting 406 (not Acceptable)] (http://stackoverflow.com/questions/7462202/spring-json-request-getting-406-non accettabile) – kdgregory