昨天写程序,利用ajax向后台发送json对象请求的时候出现了415错误,网上的方法试了一大堆都不管用,后台我将springMVC的配置文件改成了前的,就可以了
springMVC更改之前
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
">
<!-- 自动扫描 @Controller-->
<context:component-scan base-package="com.mingtai.mavenweb">
<!-- <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"></context:exclude-filter>-->
</context:component-scan>
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id = "stringHttpMessageConverter" class = "org.springframework.http.converter.StringHttpMessageConverter"/>
<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter"/> <!-- JSON转换器 -->
<ref bean= "stringHttpMessageConverter" />
<ref bean="jsonHttpMessageConverter" />
<!-- <ref bean= "jsonHttpMessageConverter" />
<ref bean= "formHttpMessageConverter" />-->
</list>
</property>
</bean>
<!-- 配置视图解释器 jsp -->
<!-- <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>-->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/html/"/>
<property name="suffix" value=".html"/>
<property name="contentType" value="text/html"/>
</bean>
<!-- 文件上传配置 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="UTF-8"/>
<!-- 上传文件大小限制为31M,31*1024*1024 -->
<property name="maxUploadSize" value="32505856"/>
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="4096"/>
</bean>
</beans>
更改之后
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<context:component-scan base-package="com.mingtai.mavenweb">
<!--<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"></context:exclude-filter>-->
</context:component-scan>
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
<property name="features">
<list>
<!--设置null值也要输出,fastjson默认是关闭的-->
<value>WriteMapNullValue</value>
<!--设置使用文本方式输出日期,fastjson默认是long-->
<value>WriteDateUseDateFormat</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/html/"/>
<property name="suffix" value=".html"/>
<property name="contentType" value="text/html"/>
</bean>
</beans>
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mavenweb_parent</artifactId>
<groupId>com.mavenweb</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../mavenweb_parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mavenweb_manage_web</artifactId>
<packaging>war</packaging>
<name>mavenweb_manage_web Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.mavenweb</groupId>
<artifactId>mavenweb_manage</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<dependency>
<groupId>com.github.miemiedev</groupId>
<artifactId>mybatis-paginator</artifactId>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<!-- 文件上传组件 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
<!-- 缓存 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.3</version>
</dependency>
<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-core</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.1</version>
</dependency>
<!--
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.2</version>
</dependency>-->
<!--因为页面在web工程,所以,jstl要在这里,jstl是JSP的一个标签库-->
<!-- JSTL标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>mavenweb_manage_web</finalName>
<!--<pluginManagement><!– lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) –>-->
<!--<plugins>-->
<!--<plugin>-->
<!--<artifactId>maven-clean-plugin</artifactId>-->
<!--<version>3.0.0</version>-->
<!--</plugin>-->
<!--<!– see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging –>-->
<!--<plugin>-->
<!--<artifactId>maven-resources-plugin</artifactId>-->
<!--<version>3.0.2</version>-->
<!--</plugin>-->
<!--<plugin>-->
<!--<artifactId>maven-compiler-plugin</artifactId>-->
<!--<version>3.7.0</version>-->
<!--</plugin>-->
<!--<plugin>-->
<!--<artifactId>maven-surefire-plugin</artifactId>-->
<!--<version>2.20.1</version>-->
<!--</plugin>-->
<!--<plugin>-->
<!--<artifactId>maven-war-plugin</artifactId>-->
<!--<version>3.2.0</version>-->
<!--</plugin>-->
<!--<plugin>-->
<!--<artifactId>maven-install-plugin</artifactId>-->
<!--<version>2.5.2</version>-->
<!--</plugin>-->
<!--<plugin>-->
<!--<artifactId>maven-deploy-plugin</artifactId>-->
<!--<version>2.8.2</version>-->
<!--</plugin>-->
<!--</plugins>-->
<!--</pluginManagement>-->
<resources>
<!--将java代码目录中的xml输出,默认不输出除resources目录外的xml文件-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
controller方法
@RequestMapping({"/customer/saveCustomer.do"})
@ResponseBody
public String saveCustomer(@RequestBody Customer customer) {
System.out.println("id为++++++++++++++++" + customer.getCust_name());
System.out.println("id为++++++++++++++++" + customer.getCust_id());
if (customer.getCust_id() == null) {
System.out.println("添加");
this.customerService.addCustomer(customer);
} else {
System.out.println("修改");
this.customerService.updateCustomer(customer);
}
return "VueCustomerList";
}
这里有几个依赖很特别
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.1</version>
</dependency>
又一次,我添加了下面这个jar包,竟然神奇的运行成功了,这时我将springMVC的配置文件改为以前的,看能不能运行成功,以此来判断是否是少了下面这个jar包的问题,结果失败,然后又改为了成功的那个springMVC配置文件,结果也不行了,然后,之后又把该jar包去掉,再运行,就又成功了。。。。一头雾水,到现在我都不知道怎么回事。。。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.4</version>
</dependency>
就这么神奇的好了,我自己都不知道哪里出了问题
补充:因该还是前台发送ajax请求时的问题,最近用公司的框架写东西,也出现了这个问题,首先排除了环境问题,因为公司的代码,也有用到这个的,但是完全没有问题,后台我调用了公司封装好的发送post请求的代码,就没有这个问题了。
错误的代码:
Util.httpPost(this.api.page, query, function (result) {
app.showLoading = false;
if (result.success) {
app.submitList = result.data.rows;
app.pagination.total = result.data.total;
}
});
正确的代码:
Util.httpPostJson(this.api.submitOrCancel, app.archive, function (result) {
if (result.success) {
if (status == 0) {
app.$message({message: '撤稿成功', type: 'success'});
app.getSubmitList();
console.log("执行加载函数");
}
if (status == 1) {
app.$message({message: '提交成功', type: 'success'});
app.getSubmitList();
console.log("执行加载函数");
}
httpPostJson的源码
httpPostJson: function (url, data, callback) {
axios.post(url, data)
.then(function (response) {
Util.checkHttpLogin(response, function (result) {
if (callback) callback(result, data);
});
})
.catch(function (error) {
console.log(error);
if (callback) callback({success:false, message: "网络请求异常."}, data);
});
},
checkHttpLogin源码
//检查API请求是否需要登录,如果需要登录则跳转到重新登录页面,登录成功后刷新当前页面
checkHttpLogin: function (response, callback) {
if (browser.versions.mobile) {
if (browser.versions.iPhone | browser.versions.android) {
Util.checkHttpMobileLogin(response, callback);
} else if (browser.versions.app) {
// Util.checkHttpAppLogin(response, callback);
}
} else {
Util.checkHttpPCLogin(response, callback);
}
}
推荐几个,比较好的在使用了@Request后出现415问题的帖子
链接1:https://blog.youkuaiyun.com/yixiaoping/article/details/45281721#comments
链接2:https://www.jianshu.com/p/a5ca1fc63f81
链接3:https://blog.youkuaiyun.com/kility/article/details/70144931
链接4:https://www.cnblogs.com/qq78292959/p/3761646.html