Ich bin neu im Frühling und versuche derzeit, HTTP POST anzufordern. Application/x-www-form-url encoded, aber wenn ich dies in meinen Kopfzeilen belasse, wird der Frühling nicht erkannt und 415 Unsupported Media Type
.__ x-www-form-urlencoded
org.springframework.web.HttpMediaTypeNotSupportedException: Content Typ 'application/x-www-form-urlencoded' wird nicht unterstützt
Kann jemand wissen, wie man es löst? Bitte kommentiere mich.
Ein Beispiel für meinen Controller ist:
@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;
}
Das Problem ist, dass, wenn wir application/x-www-form-urlencoded verwenden, Spring es nicht als RequestBody versteht. Wenn Sie also diese Verwenden möchten, müssen Sie die Annotation @RequestBody entfernen.
Dann versuche folgendes:
@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;
}
Beachten Sie, dass die Anmerkung @RequestBody entfernt wurde.
Die Lösung finden Sie hier https://github.com/spring-projects/spring-framework/issues/22734
sie können zwei separate Zuordnungen für Post-Requests erstellen. Zum Beispiel.
@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();
}
sie sollten @RequestBody durch @RequestParam ersetzen und keine Parameter mit einer Java-Entität akzeptieren.
Dann ist dein Controller wahrscheinlich so:
@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;
}
ersetzen Sie contentType: "application/x-www-form-urlencoded" durch dataType: "text", da Wildfly 11 den genannten Inhaltstyp nicht unterstützt.
Am einfachsten ist es, den Inhaltstyp Ihrer Ajax-Anfrage auf "application/json; charset = utf-8" zu setzen und dann die API-Methode json verwenden zu lassen. So was
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){
Ich schätze, das Problem ist, dass Spring Boot Probleme beim Senden von Formulardaten hat, die nicht über eine AJAX-Anfrage json json sind.
Sie müssen Spring mitteilen, welcher Eingabe-Inhaltstyp von Ihrem Dienst unterstützt wird. Dies ist mit dem Anmerkungselement "verbraucht" möglich, das dem Header "Content-Type" Ihrer Anforderung entspricht.
@RequestMapping(value = "/", method = RequestMethod.POST, consumes = {"application/x-www-form-urlencoded"})
Es wäre hilfreich, wenn Sie Ihren Code gepostet hätten.