Sto lavorando su un servizio Web Java restful. Ho funzionato per la richiesta GET, ma la richiesta POST non funziona. La mia classe Controller è RestController
. Finora li ho eseguiti:Stato HTTP 415 - l'entità richiesta è in un formato non supportato
@RequestMapping(value = "/api/signup", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public long signUp(@ModelAttribute ApiMemberModel apiMember) {
memberService = new MemberDetailsService();
Member m = memberService.createMember(apiMember.getUsername(), apiMember.getPassword(), apiMember.getEmail(), "");
return m.getId();
}
Inoltre, ha provato RequestBody anziché ModelAttribute.
Io uso Postman extension per inviare la richiesta POST. Per esempio:
{
"username": "asd",
"password": "sfsdg",
"email": "[email protected]"
}
ma ho l'errore:
description The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
Che cosa sto facendo di sbagliato? classe del modello è:
public class ApiMemberModel {
private String username;
private String password;
private String email;
public ApiMemberModel(String username, String password, String email) {
this.username = username;
this.password = password;
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Mostraci il codice della tua classe modello – Marged
@Marged aggiunto ora. – nope
_ "Ho provato anche RequestBody invece di ModelAttribute." _, Giusto, ma hai bisogno anche di un costruttore no-arg sul modello, altrimenti Jackson non può crearlo. Oppure usa '@ JsonCreator' –