Sto utilizzando Spring MVC con JSON come specificato in Ajax Simplification Spring 3.0 article.mvc molla non restituisce contenuto json - errore 406
Dopo tanti tentativi e varianti del mio codice in base ai consigli trovati su vari forum, il mio codice continua a non funzionare.
Continuo a ottenere il seguente errore: (406) La risorsa identificata da questa richiesta è solo in grado di generare risposte con caratteristiche non accettabili in base alla richiesta "accetta" intestazioni().
Ho nel mio file appconfig.xml come richiesto.
app-config.xml
<context:component-scan base-package="org.ajaxjavadojo" />
<!-- Configures Spring MVC -->
<import resource="mvc-config.xml" />
MVC-config.xml
<mvc:annotation-driven />
<!-- Forwards requests to the "/" resource to the "index" view -->
<mvc:view-controller path="/" view-name="index"/>
<!-- 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>
</bean>
Questo è quello che ho per il mio controller
@Controller
@RequestMapping (value = "/convert")
public class ConversionController {
@RequestMapping(method=RequestMethod.GET)
public String getConversionForm(){
return "convertView";
}
@RequestMapping(value = "/working", headers="Accept=application/json", method=RequestMethod.GET)
public @ResponseBody Conversion getConversion(){
Conversion d = new Conversion("d");
return d;
}
}
chiamata jsp jquery
function convertToDecimal(){
$.getJSON("convert/working", {key: "r"}, function(aConversion){
alert("it worked.");
$('#decimal').val(aConversion.input);
});
}
Apprezzerei molto qualsiasi input su questo problema. Grazie
Grazie mille. Secondo l'articolo, l'intestazione di accettazione dovrebbe essere impostata su */*. così ho aggiornato il mapping della richiesta a: @RequestMapping (value = "/ working", headers = "Accept = */*", method = RequestMethod.GET) e ora funziona :)) – serena