You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用HTTP Delete方法遇类型转换异常,求Postman正确传参方式

解决Delete请求类型转换异常及Postman参数传递问题

问题根源

  • 你的Controller方法deleteByStudentName使用@RequestBody注解,期望接收JSON格式的请求体来转换为Student对象,但你实际通过路径参数传递姓名,Spring无法将路径参数转换为Student实例,触发类型转换异常。
  • 请求路径不匹配:Controller映射的是/deleteStudent,但你访问的是/deleteStudentByName/{name},请求无法正确映射到目标方法,进一步引发异常。

方案一:用路径参数传递姓名(修改Controller)

调整Controller方法,通过@PathVariable接收路径参数,同时修正映射路径:

@RestController
public class DeleteControllerForStudent {

    @Autowired
    StudentService stdService;

    // 调整映射路径,添加姓名占位符
    @DeleteMapping("/deleteStudentByName/{studentName}")
    public void deleteByStudentName(@PathVariable String studentName) {
        // 根据你的Service逻辑调整,比如创建Student对象或直接传姓名
        Student student = new Student();
        student.setName(studentName);
        stdService.deleteStudent(student);
        // 如果Service有直接按姓名删除的方法,也可以直接调用:stdService.deleteByName(studentName);
    }
}

Postman操作步骤:

  • 请求方法选DELETE
  • 请求URL填:localhost:8080/deleteStudentByName/Tushar%20Joshi(空格用%20转义,或者直接在Postman路径参数里输入带空格的姓名,Postman会自动处理)
  • 不需要填写请求体,直接点击发送即可。

方案二:保持RequestBody接收(传JSON请求体)

如果要继续用@RequestBody接收Student对象,需在Postman中传递JSON格式的请求体:

Controller保持原有代码(确保路径正确)

@RestController
public class DeleteControllerForStudent {

    @Autowired
    StudentService stdService;

    @DeleteMapping("/deleteStudent")
    public void deleteByStudentName(@RequestBody Student student) {
        stdService.deleteStudent(student);
    }
}

Postman操作步骤:

  • 请求方法选DELETE
  • 请求URL填:localhost:8080/deleteStudent
  • 切换到Body标签,选raw,格式选择JSON
  • 输入框填写JSON格式的Student对象:
{
    "name": "Tushar Joshi"
    // 如果Student类有其他必填字段,一并添加
}
  • 点击发送即可。

内容的提问来源于stack exchange,提问作者Shivam Vishwakarma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 13:03:19