问题:使用json注解@RequestBody以及@Response遇到的405、415错误
由于修改通过了就懒得找错误时的代码跟图片了,,博主搜索翻墙看了几十个博客,综合各种各样的说法最终还是没得到结果。最后只能jar换包结果。。。他么居然行了!!!debug了一天半都没发现的问题居然是包,我去,可能是版本老或是不兼容,导入错之类,但在玄学的见证下还是成功了,就懒得去搞了,毕竟还有毕业论文要弄。所以开门见山。。。
ps:我的代码在后边,
常见问题一:驱动包没导入或者没导对
这是修改前用的包jackson-core-asl-1.9.11.jar和jackson-mapper-asl-1.9.11.jar
ps:感觉json包的很不友好,资源难找,csdn的又要积分
这是修改后用的包fastjson-1.2.7.jar(百度就可以有免费的下载)
问题二:前端ajax发送contentType错误
方法:由于我第一次接触ajax发现很多人都是直接说这个错误补贴代码,搞得我一脸懵逼,所以我贴图比较好点,代码下边
代码
function requestJson(){
var var1 = {"name":"苹果","price":6666};
$.ajax({
type:"post",
url:'${pageContext.request.contextPath }/requestJson.action',
contentType:'application/json;charset=utf-8',
data:JSON.stringify(var1),
success:function(data){//返回json结果
alert(data);
}
});
}
注意
没错,不仅请求头,响应头contentType也是这个,text/html;charset=UTF-8至于怎么设置最后边贴个人代码会说明,这里先不重复
问题三,读不到js文件,被拦截了
遇到了直接输入js的url但404找不到文件,最后解决不过治根不治本,
方法:添加spring-mvc.xml配置文件下添加不拦截静态文件的代码
代码
<mvc:default-servlet-handler/>
问题四:配置maven依赖
方法:没用maven依赖,跳过,用的自己百度,总不能让我帮你收集吧。。。
问题五 。。。呃想不到比较特别留意的了。。
那些get/post方法写错的,语法拼写写错的,url拦截/*.html之类感觉没人会犯这些错误就不提了
以下个人代码:
将以截图+代码(方便copy)的方式展示
开发环境spring包为4.1(myeclipse自带包,基本能满足大部分人不要不要的了,3.1及以前版本跟jdk1.8不兼容),jdk1.8,myeclipse 2017(破解版),tomcat9.0,没用maven依赖,前端有用jstl
前端jsp页面,有些没用的、放不下的、重复的就不截图了
图片
代码
<%@ page language="java" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>json测试</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
function requestJson(){
var var1 = {"name":"苹果","price":6666};
$.ajax({
type:"post",
url:'${pageContext.request.contextPath }/requestJson.action',
contentType:'application/json;charset=utf-8',
data:JSON.stringify(var1),
success:function(data){//返回json结果
alert(data);
}
});
}
</script>
</head>
<body>
<input type="button" onclick="testJson()" value="测试请求"/><br>
<input type="button" onclick="requestJson()" value="请求json"/><br>
<input type="button" onclick="responseJson()" value="请求key/value"/><br>
</body>
</html>
配置文件 spring-mvc.xml
图片
代码
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd ">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置jsp路径的前缀-->
<!--配置jsp路径的后缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<context:component-scan base-package="com.ssm.controller"/>
<mvc:annotation-driven >
<mvc:message-converters register-defaults="true">
<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="fastJsonHttpMessageConverter"
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<!-- 配置响应头contentType -->
<value>application/json;charset=utf-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:default-servlet-handler/>
</beans>
supportedMediaTypes,如果笔者没推错的话,这里就是之前问题二说的配置响应头,因为试了下更改然后页面的响应头也跟着改了,然后自然就servic 415炸了。我这里使用的是<mvc:annotation-driven >开发,如果有使用注入开发或者非注解开发的方式需要自己百度找对应的方法
JsonTest.java
注释掉的很多是网上找的方法又用不上的
图片
代码
package com.ssm.controller;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.ssm.po.ItemsCustom;
/**
*
* <p> Title: JsonTest</p>
* <p> Description: </p>
* @author 古大帅哥
* @date 2018年11月30日 下午4:16:05
*/
@RestController
public class JsonTest {
//请求json串(商品信息),输出json(商品信息)
//@RequestBody将请求的商品信息的json串转成itemsCustom对象
//@ResponseBody将itemsCustom转成json输出
@RequestMapping(value="/requestJson.action")//,produces="application/json;charset=UTF-8"
public @ResponseBody ItemsCustom requestJson(@RequestBody ItemsCustom itemsCustom,
HttpServletResponse response){
/*response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Content-Type", "application/json;charset=UTF-8");*/
return itemsCustom;
}
}
感觉还有很多需要补充修改的,不过毕竟小白初学这块,等日后有时间再回头修改吧,发现有错误点的大佬欢迎指出!!!
快凌晨四点了,睡了睡了!!!偷偷躲过起床练球的科比,再不睡要猝死了 ()´д`()
后待续