問題:使用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;
}
}
感覺還有很多需要補充修改的,不過畢竟小白初學這塊,等日後有時間再回頭修改吧,發現有錯誤點的大佬歡迎指出!!!
快凌晨四點了,睡了睡了!!!偷偷躲過起牀練球的科比,再不睡要猝死了 ()´д`()
後待續