Tôi đang làm việc trên dịch vụ web an toàn java. Tôi nhận được nó làm việc cho yêu cầu GET, nhưng yêu cầu POST không hoạt động. Lớp Trình điều khiển của tôi là RestController
. Tôi đã thực hiện những việc này cho đến thời điểm này:Trạng thái HTTP 415 - yêu cầu đối tượng có định dạng không được hỗ trợ
@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();
}
Cũng đã thử RequestBody thay vì ModelAttribute.
Tôi sử dụng Postman extension để gửi yêu cầu POST. Ví dụ:
{
"username": "asd",
"password": "sfsdg",
"email": "[email protected]"
}
Nhưng tôi nhận được lỗi:
description The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
Tôi đang làm gì sai? Mô hình lớp học là:
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;
}
}
Hiển thị cho chúng tôi mã của lớp mô hình của bạn – Marged
@Được thêm vào ngay bây giờ. – nope
_ "Cũng thử RequestBody thay vì ModelAttribute." _, Đúng vậy nhưng bạn cũng cần một nhà xây dựng no-arg trên mô hình hoặc người nào khác Jackson không thể tạo ra nó. Hoặc sử dụng '@ JsonCreator' –