Spring 4.0.x JSON/Ajax HTTP/1.1 406 Not Acceptable

2020-05-06 12:17发布

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)

Error Message 01

Error Message 02

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.

1条回答
闹够了就滚
2楼-- · 2020-05-06 12:30

For the audience.

The error was in the same URL. It contains .htm

Therefore for all the developers be sure to remove it

From

@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());
}

To

@RequestMapping(value="/findallproductobycategoria", 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());
}

From:

$.ajax({
    url: "/spring-utility/facturaajax/findallproductobycategoria.htm" ,
    data: JSON.stringify(json),
    dataType: 'json',
    type: "POST",

To:

$.ajax({
    url: "/spring-utility/facturaajax/findallproductobycategoria" ,
    data: JSON.stringify(json),
    dataType: 'json',
    type: "POST",

Because I have

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    Map<String,MediaType> mediaTypes = new LinkedHashMap<>();
    mediaTypes.put("json", MediaType.APPLICATION_JSON);
    mediaTypes.put("xml", MediaType.APPLICATION_XML);
    configurer.mediaTypes(mediaTypes);
    configurer.defaultContentType(MediaType.TEXT_HTML);
}

Spring gives more preference about the URL .extension than the header content

查看更多
登录 后发表回答