问题描述
我是 spring 的新手,目前正在尝试执行 HTTP POST 请求应用程序/x-www-form-url 编码,但是当我将其保留在我的标头中时,spring 无法识别它并说 415 不支持的媒体类型
对于 x-www-form-urlencoded
Am new to spring currently am trying to do HTTP POST request application/x-www-form-url encoded but when i keep this in my headers then spring not recognizing it and saying 415 Unsupported Media Type
for x-www-form-urlencoded
org.springframework.web.HttpMediaTypeNotSupportedException:内容不支持输入application/x-www-form-urlencoded"
有人知道怎么解决吗?请评论我.
Can any one know how to solve it? please comment me.
我的控制器的一个例子是:
An example of my controller is:
@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;
}
推荐答案
问题是当我们使用application/x-www-form-urlencoded时,Spring并没有把它理解为一个RequestBody.所以,如果我们想使用这个我们必须删除 @RequestBody 注释.
The problem is that when we use application/x-www-form-urlencoded, Spring doesn't understand it as a RequestBody. So, if we want to use thiswe must remove the @RequestBody annotation.
然后尝试以下操作:
@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
这篇关于内容类型为 application/x-www-form-urlencoded 的 Http Post 请求在 Spring 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!