您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“SpringMVC @RequestBody自動(dòng)轉(zhuǎn)json Http415錯(cuò)誤如何解決”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
項(xiàng)目中想用@RequestBody直接接收json串轉(zhuǎn)成對(duì)象
網(wǎng)上查了使用方法,看著非常簡單,不過經(jīng)過測試很快發(fā)現(xiàn)頁面直接報(bào)415錯(cuò)誤。
<body> <h2>HTTP Status 415 - </h2> <HR size="1" noshade="noshade"> <p> <b>type</b> Status report </p> <p> <b>message</b> <u></u> </p> <p> <b>description</b> <u>The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.</u> </p> <HR size="1" noshade="noshade"> <h4>Apache Tomcat/6.0.41</h4> </body>
經(jīng)過一通查,多半的解決方法實(shí)說header里的 Content-Type 一定 application/json
但是問題依然沒有解決。
最后在《Spring in Action》里找到一個(gè)信息
有兩個(gè)前提條件:
The request’sContent-Typeheader must be set toapplication/json.
The JacksonJSONlibrary must be available on the application’s classpath.
我滿足了第一個(gè),所以在classpath中添加了一個(gè)jar。問題解決了。
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.3</version> </dependency>
所以如果大家遇到了同樣的問題,可以先排除一下這兩個(gè)因素。
還有一種情況,在以上兩個(gè)條件都滿足的情況下,還是報(bào)同樣的錯(cuò)誤。
在springmvc的配置文件中必須有:
<!-- 默認(rèn)的注解映射的支持 --> <mvc:annotation-driven />
如果沒有這個(gè)配置也是會(huì)報(bào)這個(gè)錯(cuò)的!
為什么會(huì)引入jackson-databind包呢,因?yàn)槟J(rèn)的配置會(huì)用到:
com.fasterxml.jackson.databind.ObjectMapper
<mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="com.fasterxml.jackson.databind.ObjectMapper"> <property name="dateFormat"> <bean class="java.text.SimpleDateFormat"> <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" /> </bean> </property> </bean> </property> </bean> </mvc:message-converters>
Spring mvc是一個(gè)非常輕量的mvc框架,注解可以大大減少配置,讓請(qǐng)求的攔截變得比較簡單。這次記錄下@RequestBody 注解接收參數(shù)尤其是數(shù)組參數(shù)的用法。
關(guān)于容器的配置不再多說,這里寫出spring-servlet.xml的sechme:
<?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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"> <!-- 掃描包注解 --> <context:component-scan base-package="xxxx"></context:component-scan> <!-- mvc注解功能啟動(dòng) --> <mvc:annotation-driven /> </beans>
只要對(duì)應(yīng)包名下面的添加注解即可掃描到對(duì)應(yīng)的控制器,一般采用@Controller
RequestBody接收基本類型
@Controller public class TestController { // url請(qǐng)求攔截 @RequestMapping("test/test.do") @ResponseBody // 返回參數(shù)為JSON public void test(@RequestBody String name) { System.out.println("getParams : " + name); }![](https://img-blog.csdn.net/20161114115809292) }
@RequestBody只要接收POST請(qǐng)求Body里的數(shù)據(jù)。
這樣發(fā)送請(qǐng)求,即可在java控制臺(tái)中打印:
getParams : {"name":"micro"}
@RequestBody接收基本數(shù)組
然后我們接收基本類型數(shù)組:
@RequestMapping("test/test.do") @ResponseBody public void test(@RequestBody List<String> nameList) { System.out.println("getParams : " + nameList); }
這樣即可獲取到參數(shù),不要body里寫成了{(lán)“nameList”:[“name1”,“name2”]}這樣會(huì)拋出異常。
@RequestBody是對(duì)應(yīng)的POST請(qǐng)求的body,body即是獲取的參數(shù),如果想通過參數(shù)去獲取,則要使用@RequestParams 注解:
@RequestMapping("test/test.do") @ResponseBody public void test(@RequestParam("name") String name) { System.out.println("getParams : " + name); }
注意是GET請(qǐng)求,參數(shù)直接放到URL后面,這樣就可以使用@RequestParams獲取到對(duì)應(yīng)參數(shù)名的參數(shù)值。
如果是復(fù)雜的對(duì)象。
@RequestBody的使用。
定義model:
class Person { private Long id; private String name; // setter getter }
@RequestBody接收復(fù)雜對(duì)象
接收參數(shù)的方式
@RequestMapping("test/test.do") @ResponseBody public void test(@RequestBody Person person) { System.out.println("getParams : " + person.getId() + " ," + person.getName()); }
即可獲取到參數(shù),body里的參數(shù)會(huì)自動(dòng)匹配到person的屬性并賦值。
注意名字要與對(duì)象的屬性變量名一致。否則獲取不到參數(shù),例如這里就不能在body里寫成{“i”:1,“name”:“micro”},這樣獲取到person的id為null。
@RequestBody接收復(fù)雜對(duì)象數(shù)組
如果是復(fù)雜對(duì)象數(shù)組:
@RequestMapping("test/test.do") @ResponseBody public void test(@RequestBody List<Person> personList) { for (Person p : personList) { System.out.println(p.getId() + " ," + p.getName()); } }
請(qǐng)求方式如下,注意body里的格式是[]數(shù)組。
控制臺(tái)打?。?/p>
1 ,micro
2 ,micro2
“SpringMVC @RequestBody自動(dòng)轉(zhuǎn)json Http415錯(cuò)誤如何解決”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。