簡體   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