繁体   English   中英

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

[英]Http Post request with content type application/x-www-form-urlencoded not working in 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;
}

问题在于,当我们使用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

你应该@RequestParam取代@RequestBody,并且不接受与Java实体参数。

那么你的控制器可能是这样的:

@RequestMapping(value = "/patientdetails", method = RequestMethod.POST, 
consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
public @ResponseBody List<PatientProfileDto> getPatientDetails(
    @RequestParam Map<String, String> name) {
   List<PatientProfileDto> list = new ArrayList<PatientProfileDto>();
   ...
   PatientProfileDto patientProfileDto = mapToPatientProfileDto(mame);
   ...
   list = service.getPatient(patientProfileDto);
   return list;
}

解决方案可以在这里找到https://github.com/spring-projects/spring-framework/issues/22734

您可以创建两个单独的发布请求映射。 例如。

@PostMapping(path = "/test", consumes = "application/json")
public String test(@RequestBody User user) {
  return user.toString();
}

@PostMapping(path = "/test", consumes = "application/x-www-form-urlencoded")
public String test(User user) {
  return user.toString();
}

最简单的做法是将 ajax 请求的内容类型设置为"application/json; charset=utf-8" ,然后让您的 API 方法使用 JSON。 像这样:

var basicInfo = JSON.stringify({
    firstName: playerProfile.firstName(),
    lastName: playerProfile.lastName(),
    gender: playerProfile.gender(),
    address: playerProfile.address(),
    country: playerProfile.country(),
    bio: playerProfile.bio()
});

$.ajax({
    url: "http://localhost:8080/social/profile/update",
    type: 'POST',
    dataType: 'json',
    contentType: "application/json; charset=utf-8",
    data: basicInfo,
    success: function(data) {
        // ...
    }
});


@RequestMapping(
    value = "/profile/update",
    method = RequestMethod.POST,
    produces = MediaType.APPLICATION_JSON_VALUE,
    consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ResponseModel> UpdateUserProfile(
    @RequestBody User usersNewDetails,
    HttpServletRequest request,
    HttpServletResponse response
) {
    // ...
}

我想问题是 Spring Boot 在通过 ajax 请求提交不是 JSON 的表单数据时存在问题。

注意:ajax 的默认内容类型是"application/x-www-form-urlencoded"

从方法中的使用参数中删除 @ResponseBody 注释。 像这样;

   @Autowired
    ProjectService projectService;

    @RequestMapping(path = "/add", method = RequestMethod.POST)
    public ResponseEntity<Project> createNewProject(Project newProject){
        Project project = projectService.save(newProject);

        return new ResponseEntity<Project>(project,HttpStatus.CREATED);
    }

您必须告诉 Spring 您的服务支持什么输入内容类型。 您可以使用与您的请求的“Content-Type”标头相对应的“consumes”注释元素来执行此操作。

@RequestMapping(value = "/", method = RequestMethod.POST, consumes = {"application/x-www-form-urlencoded"})

如果您发布了代码,这将很有帮助。

替换 contentType : "application/x-www-form-urlencoded", by dataType : "text" 因为 wildfly 11 不支持提到的内容类型..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM