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

springboot中PostMapping正常接收json參數(shù)后返回404問題

 更新時(shí)間:2022年05月06日 16:47:53   作者:陸沙  
這篇文章主要介紹了springboot中PostMapping正常接收json參數(shù)后返回404問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

PostMapping接收json參數(shù)后返回404

問題描述

js中傳遞json數(shù)據(jù)給后端,后端可以正常接收參數(shù),但返回404。

js

? ? ? ? ? ? ? ? function rootConfirm(ids, types) {
? ? ? ? ? ? ? ? ? ? $.tool.confirm("確定結(jié)束" + options.modalName + "?", function () {
? ? ? ? ? ? ? ? ? ? ? ? $.ajax({
? ? ? ? ? ? ? ? ? ? ? ? ? ? type: "post",
? ? ? ? ? ? ? ? ? ? ? ? ? ? url: options.confirmUrl,
? ? ? ? ? ? ? ? ? ? ? ? ? ? traditional: true,
? ? ? ? ? ? ? ? ? ? ? ? ? ? data: {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'ids': ids,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'types': types
? ? ? ? ? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? ? ? ? ? success: function (json) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? $.tool.ajaxSuccess(json);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? $.tableUtil.refresh();
? ? ? ? ? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? ? ? ? ? error: $.tool.ajaxError
? ? ? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? }, function () {}, 5000);
? ? ? ? ? ? ? ? }

后臺(tái)

?@RequiresPermissions(value = {"root_orders:confirm", "root_orders:batchConfirm"}, logical = Logical.OR)
?@PostMapping(value="/root_orders/confirm")
?public ResponseVO rootConfirmOrder(Long[] ids, String[] types) {
? if (ids == null || types == null)
? ?return ResultUtil.error(500, "請(qǐng)至少選擇一個(gè)訂單");
? for (int i = 0; i < ids.length; i++) {
? ?/*可以正常打印*/
? ?System.out.println("" + ids[i] + ":" + types[i]);
? }
? return ResultUtil.success("成功結(jié)束 [" + ids.length + "] 個(gè)訂單");
?}

解決

添加**@ResponseBody**注解。因?yàn)槲业暮瘮?shù),所在的類注解是@Controller,但函數(shù)是要返回?cái)?shù)據(jù)而非視圖的。

補(bǔ)充

  • @RestController

這個(gè)注解相當(dāng)于@ResponseBody 和 @Controller兩個(gè)注解的組合,不返回視圖,只返回?cái)?shù)據(jù)。如果一個(gè)類上加了這個(gè)注解,那么這個(gè)類的函數(shù)都是返回不了視圖的,return “redirect:/XXX/details”;也會(huì)只在頁面上顯示return的字符串。

解決方法是把類上的注解改為@Controller,然后給不返回視圖,只返回?cái)?shù)據(jù)的函數(shù)加上注解@ResponseBody。

@PostMapping注解解析

開發(fā)過程IDEA提示如將

@RequestMapping(value="/abc" , method = “RequestMethod.POST”)

替換成@PostMapping?,F(xiàn)對(duì)@PostMapping的實(shí)現(xiàn)。

@PostMapping是一個(gè)復(fù)合注解,Spring framework 4.3引入了@RequestMapping注釋的變體,以更好地表示帶注釋的方法的語義,作為@RequestMapping(method = RequestMethod.POST)的快捷方式。

也就是可以簡化成@PostMapping(value="/abc" )即可,主要是方便識(shí)記。

下面很多方法都是對(duì)應(yīng)著@RequestMapping的標(biāo)記的別名。

@RequestMapping(value = “”, path = “”, params = “”, headers = “”,consumes = “”, produces = “”)
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = RequestMethod.POST)
public @interface PostMapping {
?? ?/**
?? ? * RequestMapping 的別名,
?? ? */
?? ?@AliasFor(annotation = RequestMapping.class)
?? ?String name() default "";
?? ?/**
?? ? *RequestMapping#value的別名, 默認(rèn)為空字符串,一般需要自己填寫
?? ? */
?? ?@AliasFor(annotation = RequestMapping.class)
?? ?String[] value() default {};
?? ?/**
?? ? * RequestMapping#path的別名
?? ? */
?? ?@AliasFor(annotation = RequestMapping.class)
?? ?String[] path() default {};
?? ?/**
?? ? * RequestMapping#params的別名
?? ? */
?? ?@AliasFor(annotation = RequestMapping.class)
?? ?String[] params() default {};
?? ?/**
?? ? * RequestMapping#headers的別名
?? ? */
?? ?@AliasFor(annotation = RequestMapping.class)
?? ?String[] headers() default {};
?? ?/**
?? ? * RequestMapping#consumes的別名
?? ? */
?? ?@AliasFor(annotation = RequestMapping.class)
?? ?String[] consumes() default {};
?? ?/**
?? ? * RequestMapping#produces的別名
?? ? */
?? ?@AliasFor(annotation = RequestMapping.class)
?? ?String[] produces() default {};
}

其他變體如下:

@GetMapping、@PutMapping、@PatchMapping和@DeleteMapping,與@PostMapping實(shí)現(xiàn)類似 

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

相關(guān)文章

最新評(píng)論

交口县| 公主岭市| 澄迈县| 晋江市| 香格里拉县| 清河县| 秦皇岛市| 塔城市| 高雄县| 新竹市| 皋兰县| 奎屯市| 花垣县| 平邑县| 林西县| 邢台市| 永仁县| 上犹县| 塘沽区| 广汉市| 利辛县| 康平县| 宜良县| 盱眙县| 平定县| 贵港市| 右玉县| 贺州市| 连山| 繁昌县| 耿马| 漳浦县| 聂荣县| 财经| 平舆县| 沁水县| 常德市| 丰原市| 彰武县| 石台县| 遂平县|