好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

springboot中PostMapping正常接收json参数后返回404问题

PostMapping接收json参数后返回404

问题描述

js中传递json数据给后端,后端可以正常接收参数,但返回404。

js

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

                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);

                }

后台

?

1

2

3

4

5

6

7

8

9

10

11

  @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**注解。因为我的函数,所在的类注解是@Controller,但函数是要返回数据而非视图的。

补充

@RestController

这个注解相当于@ResponseBody 和 @Controller两个注解的组合,不返回视图,只返回数据。如果一个类上加了这个注解,那么这个类的函数都是返回不了视图的,return [redirect:/XXX/details];也会只在页面上显示return的字符串。

解决方法是把类上的注解改为@Controller,然后给不返回视图,只返回数据的函数加上注解@ResponseBody。

@PostMapping注解解析

开发过程IDEA提示如将

?

1

@RequestMapping (value= "/abc" , method = [RequestMethod.POST])

替换成@PostMapping。现对@PostMapping的实现。

@PostMapping是一个复合注解,Spring framework 4.3引入了@RequestMapping注释的变体,以更好地表示带注释的方法的语义,作为@RequestMapping(method = RequestMethod.POST)的快捷方式。

也就是可以简化成@PostMapping(value="/abc" )即可,主要是方便识记。

下面很多方法都是对应着@RequestMapping的标记的别名。

?

1

@RequestMapping (value = [], path = [], params = [], headers = [],consumes = [], produces = [])

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

@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实现类似 

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/pxy7896/article/details/107109613

查看更多关于springboot中PostMapping正常接收json参数后返回404问题的详细内容...

  阅读:15次