解讀SpringBoot接收List<Bean>參數(shù)問題(POST請求方式)
接收List<Bean>參數(shù)問題(POST請求方式)
接部門排序后,前端已經(jīng)返回了我們想要的數(shù)據(jù)格式(采用FormData方式),但是后臺一直遲遲無法接收到我想要的(不想用json字符串來接收,還得轉(zhuǎn)),一臉問號,這是什么操作?一定是姿勢不對
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:226)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:157)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:130)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:124)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:131)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:981)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:884)
看過很多文章,有的說建立一個實體類,把list對象放進去當(dāng)成屬性,然后會自動映射賦值,但那樣有點麻煩,不想弄
上述異常的最終解決方案為前端Ajax請求時,添加contentType即可
jQuery.ajax({
? ? async: false,
? ? cache: false,
? ? type: 'POST',
? ? //添加
? ? contentType: "application/json; charset=utf-8",
? ? dataType: 'json',
? ? data: JSON.stringify(paramData),
? ? url: '...',
? ? error: function () {
? ? ? ??
? ? },
? ? success: function (data) {
? ? ? ? //console.log(data)
? ? }
});后臺使用@RequestBody注解對屬性做映射即可,如下
? ? /**
? ? ?* 修改部門排序
? ? ?*
? ? ?* @param request
? ? ?* @param depts
? ? ?* @return
? ? ?*/
? ? @PostMapping(value = "/sort")
? ? public DDResult updateDeptTreeSort(HttpServletRequest request, @RequestBody List<Dept> depts) {
? ? ? ? return DDResult.success(deptService.updateDeptTreeSort(depts));
? ? }嘗試過用如下方式傳遞,然鵝還是失敗了
dept[0].id:10
dept[0].pid:null
dept[0].order:0
dept[1].id:20
dept[1].pid:null
dept[1].order:1
springboot接口參數(shù)為List
簡單接口參數(shù)
springboot接口訪問,如果是走h(yuǎn)ttp請求,那么參數(shù)會默認(rèn)轉(zhuǎn)換成鍵值對格式,或者是json字符串形式的。

今天就遇到一個場景,我需要一個list結(jié)構(gòu)的參數(shù),list里面又是對象,那我們該怎么傳參呢?
我們不能再瀏覽器上直接拼接參數(shù)發(fā)請求了,例如:

這樣的格式是不被允許的,請求會報錯。
那么我們轉(zhuǎn)換思路,既然是json格式的參數(shù),那我們就傳json格式
設(shè)置headers
Content-type 的值為application/json

組織傳值參數(shù)list數(shù)據(jù)
在body中,傳值參數(shù),list 使用[]括起來

代碼實現(xiàn)請求
public void queryLiveCourseInfoTest(){
JSONArray jsonArray = new JSONArray();
JSONObject json1 = new JSONObject();
json1.put("dn","123");
json1.put("bossCode","34455");
jsonArray.add(json1);
JSONObject json2 = new JSONObject();
json2.put("dn","234");
json2.put("bossCode","66666");
jsonArray.add(json2);
String arr = jsonArray.toJSONString();
System.out.println("請求參數(shù):"+arr);
String url = nativUrl+"";
try {
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
HttpEntity<ElecMeterDataApi> entity = new HttpEntity<ElecMeterDataApi>(arr,headers);
String res = restTemplate.postForObject(url, entity, String.class);
log.info("結(jié)果:"+res);
} catch (RestClientException e) {
log.error("請求異常" + e.getMessage());
}
}controller層設(shè)置兩個注解
@ResponseBody 和@RequestBody
@ResponseBody沒有該注解的時候,會發(fā)生postman 請求找不到error 404;@RequestBody沒有該注解的時候,會發(fā)生參數(shù)值傳輸不過去;

這樣我們就可以實現(xiàn)通過接口直接傳遞list對象數(shù)據(jù)。
其中,list中的對象,我們在傳參數(shù)時只要組織的json格式的數(shù)據(jù)中,包含該對象的屬性以及對應(yīng)值,接口參數(shù)的對象就可以接收到該屬性值。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis連接數(shù)據(jù)庫配置的基本步驟和機制
MyBatis 是一個流行的持久層框架,它通過使用XML或注解的方式將SQL語句、存儲過程和Java方法進行綁定,從而避免了手寫大量的JDBC代碼和手動設(shè)置參數(shù)與結(jié)果集,本文給大家介紹了MyBatis連接數(shù)據(jù)庫配置的基本步驟和機制,需要的朋友可以參考下2024-05-05
Eclipse搭建spring開發(fā)環(huán)境圖文教程(推薦)
下面小編就為大家?guī)硪黄狤clipse搭建spring開發(fā)環(huán)境圖文教程(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07

