小编典典

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

spring

是新来春目前正在试图做的HTTP POST请求的应用程序/ x-WWW的形式,URL编码,但是当我守这在我的头,然后春天不承认它,并说415 Unsupported Media Typex-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;
}

阅读 1644

收藏
2020-04-13

共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;
}
2020-04-13