内容类型为 application/x-www-form-urlencoded 的 Http Post 请求在 Spring 中不起作用

新手上路,请多包涵

我是 spring 的新手,目前正在尝试进行 _HTTP POST 请求 application/x-www-form-url 编码_,但是当我将其保留在我的标头中时,spring 无法识别它并说 415 Unsupported Media Type for x-www-form-urlencoded

org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型’application/x-www-form-urlencoded’

任何人都可以知道如何解决它?请评论我。

我的控制器的一个例子是:

 @RequestMapping(
    value = "/patientdetails",
    method = RequestMethod.POST,
    headers="Accept=application/x-www-form-urlencoded")
public @ResponseBody List<PatientProfileDto> getPatientDetails(
        @RequestBody PatientProfileDto name
) {
    List<PatientProfileDto> list = new ArrayList<PatientProfileDto>();
    list = service.getPatient(name);
    return list;
}

原文由 Rajesh 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 979
1 个回答

问题是当我们使用 application/x-www-form-urlencoded 时,Spring 不会将其理解为 RequestBody。所以,如果我们想使用它,我们必须删除 @RequestBody 注释。

然后尝试以下操作:

 @RequestMapping(value = "/patientdetails", method = RequestMethod.POST,consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public @ResponseBody List<PatientProfileDto> getPatientDetails(
        PatientProfileDto name) {

    List<PatientProfileDto> list = new ArrayList<PatientProfileDto>();
    list = service.getPatient(name);
    return list;
}

请注意,删除了注释 @RequestBody

原文由 Douglas Ribeiro 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题