春4.0.x的JSON / Ajax的HTTP / 1.1 406不可接受 [英] Spring 4.0.x JSON/Ajax HTTP/1.1 406 Not Acceptable

查看:180
本文介绍了春4.0.x的JSON / Ajax的HTTP / 1.1 406不可接受的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过工作和春天4.0.5.RELEASE,Spring MVC的唯一的 Java的配置

I am working with Spring 4.0.5.RELEASE, Spring MVC through only Java Config

我在我的pom.xml如下:

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>

我使用的有关JSON的春天默认配置。在某些 @Controller 我有以下几点:

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

和工作正常。我可以在JSON返回值的浏览器中看到。直到这里一切正常。

And works fine. I can see in the browser the JSON value returned. Until here all is OK.

现在我想集成的Spring MVC +(JSON AJAX)

Now I want integrate Spring MVC + (JSON AJAX)

我有这个教程中如何引用 Spring MVC的:阿贾克斯和放大器; JQuery的

I have this tutorial how reference Spring MVC: Ajax & JQuery

好吧,我有以下有关JSON的AJAX(通过选择或组合框工作加载第二组或集合)

Ok, I have the following about JSON with AJAX (working through a select or combo box to load a second set or collection)

请注意:该URL是静态的,即使我只使用 /spring-utility/facturaajax/findallproductobycategoria.htm 问题仍然存在

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

关于控制器我有以下来处理Ajax程序

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

即使我用标题=接受=应用程序/ json的标题=内容类型=应用程序/ json的的问题仍然存在。

Even if I use headers="Accept=application/json" or headers="Content-Type=application/json" the problem persists.

该POJO是serializables

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;

问题:当我改变我的选择HTML元素的值,我总是收到 HTTP / 1.1 406不接受(见两个附件图片)

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:服务器端从不叫

BTW: The server side never is called.

我已经在SO阅读其他职位。几乎所有的人都提过杰克逊,并且基于Spring的 3.2.x的

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.

推荐答案

对于观众。

错误是在同一个URL。 它包含热媒

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

因此​​,对于所有的开发人员一定要删除它

Therefore for all the developers be sure to remove it

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

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

从:

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

要:

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

由于我

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

春给出了关于URL的更多preference。扩展名的比的的含量

这篇关于春4.0.x的JSON / Ajax的HTTP / 1.1 406不可接受的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆