溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

如何在SpringBoot后臺(tái)接收前臺(tái)傳遞的對(duì)象

發(fā)布時(shí)間:2021-01-21 15:07:22 來(lái)源:億速云 閱讀:625 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)如何在SpringBoot后臺(tái)接收前臺(tái)傳遞的對(duì)象,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

ajax方式:

$.ajax({
 url: "后臺(tái)的方式",
 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;
}

落地測(cè)試:

可以通過(guò)postman工具進(jìn)行測(cè)試

補(bǔ)充:關(guān)于SpringBoot自定義注解(解決post接收String參數(shù) null(前臺(tái)傳遞json格式))

今天遇到個(gè)問(wèn)題,接口方面的,請(qǐng)求參數(shù)如下圖為json格式(測(cè)試工具使用google的插件postman)

如何在SpringBoot后臺(tái)接收前臺(tái)傳遞的對(duì)象

后臺(tái)用字符串去接收為null

解決方案有以下幾種

1.使用實(shí)體接收(一個(gè)參數(shù),感覺(jué)沒(méi)必要)

2.使用map接收(參數(shù)不清晰,不想用)

3.自定義注解(本文采用)

第一步:

如何在SpringBoot后臺(tái)接收前臺(tái)傳遞的對(duì)象

創(chuàng)建兩個(gè)類(lèi)代碼如下:

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)
@Documented
public @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 {
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(RequestJson.class);
}
@Override
public 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);
}
}

第二步:?jiǎn)?dòng)類(lèi)添加如下代碼

如何在SpringBoot后臺(tái)接收前臺(tái)傳遞的對(duì)象

第三步:后臺(tái)請(qǐng)求(使用下圖方式接受就可以了)

如何在SpringBoot后臺(tái)接收前臺(tái)傳遞的對(duì)象

上述就是小編為大家分享的如何在SpringBoot后臺(tái)接收前臺(tái)傳遞的對(duì)象了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI