springboot中PostMapping正常接收json參數(shù)后返回404問題
PostMapping接收json參數(shù)后返回404
問題描述
js中傳遞json數(shù)據(jù)給后端,后端可以正常接收參數(shù),但返回404。
js
? ? ? ? ? ? ? ? function rootConfirm(ids, types) { ? ? ? ? ? ? ? ? ? ? $.tool.confirm("確定結束" + 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); ? ? ? ? ? ? ? ? }
后臺
?@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, "請至少選擇一個訂單"); ? for (int i = 0; i < ids.length; i++) { ? ?/*可以正常打印*/ ? ?System.out.println("" + ids[i] + ":" + types[i]); ? } ? return ResultUtil.success("成功結束 [" + ids.length + "] 個訂單"); ?}
解決
添加**@ResponseBody**注解。因為我的函數(shù),所在的類注解是@Controller,但函數(shù)是要返回數(shù)據(jù)而非視圖的。
補充
@RestController
這個注解相當于@ResponseBody 和 @Controller兩個注解的組合,不返回視圖,只返回數(shù)據(jù)。如果一個類上加了這個注解,那么這個類的函數(shù)都是返回不了視圖的,return “redirect:/XXX/details”;也會只在頁面上顯示return的字符串。
解決方法是把類上的注解改為@Controller,然后給不返回視圖,只返回數(shù)據(jù)的函數(shù)加上注解@ResponseBody。
@PostMapping注解解析
開發(fā)過程IDEA提示如將
@RequestMapping(value="/abc" , method = “RequestMethod.POST”)
替換成@PostMapping?,F(xiàn)對@PostMapping的實現(xiàn)。
@PostMapping是一個復合注解,Spring framework 4.3引入了@RequestMapping注釋的變體,以更好地表示帶注釋的方法的語義,作為@RequestMapping(method = RequestMethod.POST)的快捷方式。
也就是可以簡化成@PostMapping(value="/abc" )即可,主要是方便識記。
下面很多方法都是對應著@RequestMapping的標記的別名。
@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的別名, 默認為空字符串,一般需要自己填寫 ?? ? */ ?? ?@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實現(xiàn)類似
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
基于springboot創(chuàng)建mybatis的完整步驟
MyBatis是一款優(yōu)秀的數(shù)據(jù)庫持久層框架,相比Hibernate我更喜歡使用MyBatis,看的到SQL還是讓人更安心點,這篇文章主要給大家介紹了關于基于springboot創(chuàng)建mybatis的完整步驟,需要的朋友可以參考下2024-03-03elasticsearch bucket 之rare terms聚合使用詳解
這篇文章主要為大家介紹了elasticsearch bucket 之rare terms聚合使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11基于Lucene的Java搜索服務器Elasticsearch安裝使用教程
Elasticsearch也是用Java開發(fā)的,并作為Apache許可條款下的開放源碼發(fā)布,能夠做到實時搜索,且穩(wěn)定、可靠、快速,安裝使用方便,這里我們就來看一下基于Lucene的Java搜索服務器Elasticsearch安裝使用教程:2016-06-06