nope nope - 4 months ago 54
JSON Question

HTTP Status 415 - request entity is in a format not supported

I am working on java restful web service. I got it working for GET request, but POST request does not work. My Controller class is

RestController
. I have done these so far:

@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();
}


Also tried RequestBody instead of ModelAttribute.

I use Postman extension for sending POST request. For example:

{
"username": "asd",
"password": "sfsdg",
"email": "sdfdsf@fdsfkg.com"
}


But I get the error:

description The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.


What am I doing wrong? Model class is:

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;
}
}

Answer

I bet that call from Postman does not include Content-Type: application/json.

HTTP 415 means that the server does not understand the media format of the request. In your controller you are specifying it accepts JSON, but you have not said if the request indicated that the body is in that format. Just because you put the data in JSON format, does not mean that the server is going to recognize it, you have to indicate it in the Content-Type header.