服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - springboot中PostMapping正常接收json参数后返回404问题

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

2022-12-04 20:45陆沙 Java教程

这篇文章主要介绍了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

延伸 · 阅读

精彩推荐