I am working with Spring 4.0.5.RELEASE, Spring MVC through only Java Config
I have in my pom.xml the following:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson.version}</version>
</dependency>
Where <jackson.version>1.9.13</jackson.version>
I am using the Spring default configuration about JSON. In some @Controller
I have the following:
@RequestMapping(value="/getjsonperson",
method=RequestMethod.GET,
produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Person getJSONPerson(){
logger.info("getJSONPerson - getjsonperson");
return PersonFactory.createPerson();
}
@RequestMapping(value="/getperson.json", method=RequestMethod.GET)
public @ResponseBody Person getPersonJSON(){
logger.info("getPerson - getpersonJSON");
return PersonFactory.createPerson();
}
And works fine. I can see in the browser the JSON value returned. Until here all is OK.
Now I want integrate Spring MVC + (JSON AJAX)
I have this tutorial how reference Spring MVC: Ajax & JQuery
Ok, I have the following about JSON with AJAX (working through a select or combo box to load a second set or collection)
Note: The URL is static, even if I use only /spring-utility/facturaajax/findallproductobycategoria.htm
the problem persists
$("#categoria").change(function(event){
var json = {"id" : $(this).find("option:selected").val(), "nombre" : $(this).find("option:selected").text() };
$.ajax({
url: "http://localhost:8080/spring-utility/facturaajax/findallproductobycategoria.htm" ,
data: JSON.stringify(json),
type: "POST",
beforeSend: function(xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
},
success: function(products) {
alert("all fine!!!!");
}
});
//event.preventDefault();
});
About the Controller I have the following to handle the ajax process
@RequestMapping(value="/findallproductobycategoria.htm",
method=RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Set<Producto> findAllProductoByCategoria(@RequestBody Categoria categoria){
logger.info("findAllProductoByCategoria: {}", categoria.toString());
return this.fakeMultipleRepository.findAllProductoByCategoria(categoria.getId());
}
Even if I use headers="Accept=application/json"
or headers="Content-Type=application/json"
the problem persists.
The POJOs are serializables
public class Categoria implements Serializable {
private static final long serialVersionUID = 5655804710111228325L;
public class Producto implements Serializable {
private static final long serialVersionUID = -6362590479124787529L;
The problem: When I change the value of my select html element, I always receive the HTTP/1.1 406 Not Acceptable (see the two attached images)
BTW: The server side never is called.
I already have read other posts on SO. Practically all of them do mention about Jackson and are based on Spring 3.2.x.
Even if I add the following, the problem persists
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>${jackson.version}</version>
</dependency>
What is missing? Thank You.
For the audience.
The error was in the same URL. It contains
.htm
Therefore for all the developers be sure to remove it
From
To
From:
To:
Because I have
Spring gives more preference about the URL .extension than the header content