在请求正文中获取整数

Afsrd3:

需要获取要在数据库上删除的ID,但是我无法通过这种方式获取ID参数

@RequestMapping(value = {"/delete/search/","/delete/search"}, method = RequestMethod.DELETE)
    @ResponseBody
    public Integer deleteUser(@RequestBody Integer id_search) {
        return id_search;
    }

我收到此错误消息

"message": "JSON parse error: Can not deserialize instance of java.lang.Integer out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@1b77938; line: 1, column
pvpkiran:

这句话是错的

@RequestBody Integer id_search

这意味着Spring期望一个类型为Integerclass 的主体但是您传递的内容与Integer类不匹配。

您有很多解决方案。

  1. 删除@RequestBody并声明为

    public Integer deleteUser(Integer id_search) {...}
    

    为此,您需要将此端点称为

    http://localhost:8080/seviceRS/delete/search?id_search=2
    
  2. 您可以PathVariable像这样通过

    http://localhost:8080/seviceRS/delete/search/2
    

    为此,请像这样更改您的控制器方法

    @RequestMapping(value = 
    {"/delete/search/{id_search}"}, method = 
                                           RequestMethod.DELETE)
      @ResponseBody
      public Integer deleteUser(Integer id_search) {...}
    
  3. 如果要作为正文发送,则应创建一个与要发送的json匹配的类。

    例如。创建一个像

     class Demo {
      private int id_search;
      //Getters & Setters
     }
    

    通过这种方法,您的控制器方法看起来像

    @RequestMapping(value = {"/delete/search/}, method = 
                      RequestMethod.DELETE)
    @ResponseBody
    public Integer deleteUser(Demo demo) {
         demo.getId_Search();
    }
    

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用HttpURLConnection类在请求正文中发送数据

使用jQuery $ .ajax()时如何使用GET在请求正文中发送数据

在Spring中保存实体,并在请求正文中使用列引用子实体

如何使用HttpURLConnection在请求正文中发送数据?

Golang io.copy在请求正文中两次

在请求正文中验证日期

jQuery在请求正文中发布有效的json

$ http.get未在请求正文中发送数据

在Application Insights中将失败的请求记录在请求正文中的最佳实践是什么?

为什么Oauth2.0说“不建议在请求正文中使用客户端密码”?

在正文中获取原始请求以及正文

如何通过邮递员以JSON格式在请求正文中发送对象数组?

NGINX Lua在请求正文中搜索/替换

在Nest.js中的发布请求正文中获取XML

NestJS-在请求正文中接受多种MIME类型

如何在请求正文中使用JSON进行GET请求

Backbone.model.save在请求正文中发送整个骨架。

使用jQuery的$ .load()函数在请求正文中发送对象

JAX-RS Web服务如何仅接受在请求正文中发送的表单参数?

HttpPost 在请求正文中发布复杂的 JSONObject

在请求正文中而不是在查询字符串中发送参数

python:在请求正文中发布键/值对

如何使用Java在请求正文中使用xml创建多部分/混合请求?

为什么我无法在请求方法中从正文中获取数据

Node.js + express 如何在请求正文中添加字符串?

管道 -> 运行 -> 运行管道如何在请求正文中提供管道资源

在请求正文中获取空值

从 post 请求正文中获取值

如何在请求正文中发送原始 json