解決SpringBoot在后臺接收前臺傳遞對象方式的問題
前臺傳遞對象,不管是通過ajax請求方式,還是axios請求方式。后臺應(yīng)該怎么接收對象處理呢?
比如前臺傳遞
ajax方式:
$.ajax({ url: '后臺的方式', async: false, type: 'POST', dataType : 'json', data: JSON.stringify(formParamObj), contentType:’application/json;charset=utf-8’, success: function (data) { if (data.isSuccess) { //成功處理方式 } else if ('403' == data) { //失敗方式處理 } }});
axios方式:
let params = { key1:value1, key2:value2}axios.post/get(url,params).then(res=>{ //處理結(jié)果})解決方案:
在方法的參數(shù)前面添加注解@RequestBody就可以解決
@PostMapper('/xxx/xxxx')public List getProgramList(@RequestBody Program program){ System.out.println(program); return null;}
落地測試:
可以通過postman工具進行測試
補充:關(guān)于SpringBoot自定義注解(解決post接收String參數(shù) null(前臺傳遞json格式))
今天遇到個問題,接口方面的,請求參數(shù)如下圖為json格式(測試工具使用google的插件postman)
后臺用字符串去接收為null
解決方案有以下幾種1.使用實體接收(一個參數(shù),感覺沒必要)
2.使用map接收(參數(shù)不清晰,不想用)
3.自定義注解(本文采用)
第一步:
創(chuàng)建兩個類代碼如下:
package com.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.PARAMETER)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface RequestJson {String value();}
package com.annotation;import java.io.BufferedReader;import javax.servlet.http.HttpServletRequest;import org.springframework.core.MethodParameter;import org.springframework.web.bind.support.WebDataBinderFactory;import org.springframework.web.context.request.NativeWebRequest;import org.springframework.web.method.support.HandlerMethodArgumentResolver;import org.springframework.web.method.support.ModelAndViewContainer;import com.alibaba.fastjson.JSONObject;public class RequestJsonHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {@Overridepublic boolean supportsParameter(MethodParameter parameter) {return parameter.hasParameterAnnotation(RequestJson.class);}@Overridepublic Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {RequestJson requestJson = parameter.getParameterAnnotation(RequestJson.class);HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);BufferedReader reader = request.getReader();StringBuilder sb = new StringBuilder();char[] buf = new char[1024];int rd;while ((rd = reader.read(buf)) != -1) {sb.append(buf, 0, rd);}JSONObject jsonObject = JSONObject.parseObject(sb.toString());String value = requestJson.value();return jsonObject.get(value);}}
第二步:啟動類添加如下代碼
第三步:后臺請求(使用下圖方式接受就可以了)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. 在Android中使用WebSocket實現(xiàn)消息通信的方法詳解2. 淺談python出錯時traceback的解讀3. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向4. Python importlib動態(tài)導(dǎo)入模塊實現(xiàn)代碼5. windows服務(wù)器使用IIS時thinkphp搜索中文無效問題6. 利用promise及參數(shù)解構(gòu)封裝ajax請求的方法7. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解8. .NET中l(wèi)ambda表達式合并問題及解決方法9. JSP數(shù)據(jù)交互實現(xiàn)過程解析10. Nginx+php配置文件及原理解析
