NetworkError: 415 Unsupported Media Type - Tomcat on linux server

NetworkError: 415 Unsupported Media Type - Tomcat on linux server

我正在向 POST url 发送一些 json 数据。此代码在本地环境 (tomcat + windows) 中运行良好,但不适用于生产环境 (Tomcat + linux)。 已经提到了各种链接,但是 none 帮助了我。

问题 os 与生产环境,我也很久以前从服务器删除了一些文件,我怀疑 tomcat 可能已损坏,但其余所有 url 都在工作美好的。这是我正在做的新开发。

如果我通过 firefox rest 客户端访问,也会出现相同的行为。

错误

服务器拒绝了此请求,因为请求实体的格式不受所请求方法所请求资源的支持

Jquery代码

$.ajax({
        type : "POST",
        url : "/add",
        data : JSON.stringify(data),
        dataType: 'json',
        headers: { 
            'Accept': 'application/json',
            'Content-Type': 'application/json' 
            },
        success : function(resp) {
            console.log(resp);          
        }
    });

Spring控制器

@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody String addDishDetails(
            @RequestBody MyPojo pojo) {
        return "success";
    }

pom.xml

还在罐子下面添加

<dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.3</version>
        </dependency>

从错误中可以看出客户端没有以服务器期望的类型发送请求,客户端发送 json 并且我们定义了 cosumes=MediaType.APPLICATION_JSON_VALUE 如果我们仅将其替换为 MediaType.APPLICATION_JSON 并且您还指定了接受参数,那么要么删除它,要么将 produce 属性 添加到 application/json 在控制器中。

以上评论和回答值得一读,可能是少数情况下的解决方案。

就我而言,我有一个 属性 Map<String,String>,我需要忽略它。

所以我补充说,

@JsonIgnore
Map<String,String>