SSM post接口传递json 报错 HTTP状态 415 - 不支持的媒体类型

这篇文章是写给哪些在小破站学习ssm教程的兄弟们,我们都是萌新,大佬就让行吧感谢理解!

本文章主要讲解B站赵伟风SSM教程第108节(JSON数据的接收)

我所有的配置都跟老师一样,老师就很顺利发出去了,我的就是一直415,烦死了!!!

经过我的排查问题出现在 我们的配置类中,我们在上课的时候老是都让我们这样写的:

bash 复制代码
package org.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@EnableWebMvc
@Configuration
@ComponentScan("org.example.json")

public class MVCConfig {
    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        return new RequestMappingHandlerMapping();
    }

    @Bean
    public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
        return new RequestMappingHandlerAdapter();
    }
}

问题原因是:

这样写老师没有问题我的就报错了,我滴孩6

为什么呢??

在你手动定义 RequestMappingHandlerAdapter 和 RequestMappingHandlerMapping 时,

默认的 HttpMessageConverter 配置被覆盖或丢失。

默认情况下,Spring 自动配置 RequestMappingHandlerAdapter 并加载 HttpMessageConverter,包括 MappingJackson2HttpMessageConverter,用于处理 JSON 请求。

如果删除手动配置后,Spring 恢复默认行为,因此能够自动解析 application/json,使得 POST 请求可以正确被处理。

说人话就是,你这个不用自己配置 人家spring给你配置好了,你这样一搞,给人家的覆盖了,人家不高兴了,不给你解析Json了jackson来了都不好使!

解决方案1:更改你的配置文件

这样就可以直接解析了

bash 复制代码
package org.example.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@EnableWebMvc
@Configuration
@ComponentScan("org.example.json")
public class MVCConfig {
    // 这里不需要手动定义 requestMappingHandlerAdapter 和 requestMappingHandlerMapping
}

解决方案2:更改你的配置文件

如果确实需要手动配置 RequestMappingHandlerAdapter,你需要确认其包含 HttpMessageConverter,如 MappingJackson2HttpMessageConverter,以便处理 application/json 格式的请求和响应。

bash 复制代码
package org.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

@EnableWebMvc
@Configuration
@ComponentScan("org.example.json")
public class MVCConfig {
    
    @Bean
    public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
        RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();
        adapter.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        return adapter;
    }

}
相关推荐
游戏开发爱好者84 小时前
iOS重构期调试实战:架构升级中的性能与数据保障策略
websocket·网络协议·tcp/ip·http·网络安全·https·udp
2501_9160137414 小时前
iOS 多线程导致接口乱序?抓包还原 + 请求调度优化实战
websocket·网络协议·tcp/ip·http·网络安全·https·udp
路长且阻15 小时前
网络协议(TCP/IP、HTTP、HTTPS)
网络协议·tcp/ip·http
聽雨23715 小时前
03每日简报20250705
人工智能·社交电子·娱乐·传媒·媒体
吴free16 小时前
mac电脑wireshark快速实现http接口抓包
网络·测试工具·http·wireshark
THMOM9119 小时前
TinyWebserver学习(9)-HTTP
网络协议·学习·http
en-route1 天前
HTTP 缓存
网络协议·http·缓存
隆里卡那唔1 天前
在dify中通过http请求neo4j时为什么需要将localhost变为host.docker.internal
http·docker·neo4j
2501_915921431 天前
Fiddler 中文版怎么配合 Postman 与 Wireshark 做多环境接口调试?
websocket·网络协议·tcp/ip·http·网络安全·https·udp
~山有木兮1 天前
LiteHub中间件之限流实现
网络·http·中间件