最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringMVC @RequestBody自動轉(zhuǎn)json Http415錯(cuò)誤的解決

 更新時(shí)間:2023年04月10日 14:15:19   作者:天天  
這篇文章主要介紹了SpringMVC @RequestBody自動轉(zhuǎn)json Http415錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

SpringMVC @RequestBody自動轉(zhuǎn)json Http415錯(cuò)誤

項(xiàng)目中想用@RequestBody直接接收json串轉(zhuǎn)成對象

網(wǎng)上查了使用方法,看著非常簡單,不過經(jīng)過測試很快發(fā)現(xiàn)頁面直接報(bào)415錯(cuò)誤。

<body>
? ? ? ? <h1>HTTP Status 415 - </h1>
? ? ? ? <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">
? ? ? ? ? ? <h3>Apache Tomcat/6.0.41</h3>
</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è)配置也是會報(bào)這個(gè)錯(cuò)的!

為什么會引入jackson-databind包呢,因?yàn)槟J(rèn)的配置會用到:

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>

SpringMVC @RequestBody使用

Spring mvc是一個(gè)非常輕量的mvc框架,注解可以大大減少配置,讓請求的攔截變得比較簡單。這次記錄下@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注解功能啟動 -->
	<mvc:annotation-driven />
</beans>

只要對應(yīng)包名下面的添加注解即可掃描到對應(yīng)的控制器,一般采用@Controller

  • RequestBody接收基本類型
@Controller
public class TestController {
	// url請求攔截
	@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請求Body里的數(shù)據(jù)。

這樣發(fā)送請求,即可在java控制臺中打印:

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”]}這樣會拋出異常。

@RequestBody是對應(yīng)的POST請求的body,body即是獲取的參數(shù),如果想通過參數(shù)去獲取,則要使用@RequestParams 注解:

	@RequestMapping("test/test.do")
	@ResponseBody
	public void test(@RequestParam("name") String name) {
		System.out.println("getParams : " + name);
	}

注意是GET請求,參數(shù)直接放到URL后面,這樣就可以使用@RequestParams獲取到對應(yīng)參數(shù)名的參數(shù)值。

如果是復(fù)雜的對象。

@RequestBody的使用。

定義model:

class Person {
	private Long id;
	private String name;
	// setter getter
}
  • @RequestBody接收復(fù)雜對象

接收參數(shù)的方式

@RequestMapping("test/test.do")
	@ResponseBody
	public void test(@RequestBody Person person) {
		System.out.println("getParams : " + person.getId() + " ," + person.getName());
	}

即可獲取到參數(shù),body里的參數(shù)會自動匹配到person的屬性并賦值。

注意名字要與對象的屬性變量名一致。否則獲取不到參數(shù),例如這里就不能在body里寫成{“i”:1,“name”:“micro”},這樣獲取到person的id為null。

  • @RequestBody接收復(fù)雜對象數(shù)組

如果是復(fù)雜對象數(shù)組:

	@RequestMapping("test/test.do")
	@ResponseBody
	public void test(@RequestBody List<Person> personList) {
		for (Person p : personList) {
			System.out.println(p.getId() + " ," + p.getName());
		}
	}

請求方式如下,注意body里的格式是[]數(shù)組。

控制臺打印:

1 ,micro

2 ,micro2

總結(jié)

即完成了@RequestBody接收各種類型的參數(shù)。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

清水河县| 叶城县| 外汇| 鹤庆县| 清水河县| 沅陵县| 石河子市| 青冈县| 湖南省| 广元市| 博乐市| 石家庄市| 三门峡市| 渑池县| 金溪县| 黄梅县| 汶川县| 新安县| 简阳市| 登封市| 龙海市| 柳林县| 格尔木市| 富川| 墨玉县| 静宁县| 和平县| 和静县| 阳西县| 兴安盟| 天峨县| 海城市| 吉安市| 佛教| 遂昌县| 甘德县| 阳谷县| 务川| 海宁市| 泸水县| 中阳县|