20

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;
    }
}
nope
  • 751
  • 2
  • 12
  • 29
  • 1
    Show us the code of your model class – Marged Oct 31 '15 at 23:24
  • 2
    _"Also tried RequestBody instead of ModelAttribute."_, that's right but you also need a no-arg constructor on the model or else Jackson can't create it. Or use `@JsonCreator` – Paul Samsotha Nov 01 '15 at 01:02

5 Answers5

46

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.

vtortola
  • 34,709
  • 29
  • 161
  • 263
  • 3
    Thanks! Specifying Content-type and adding a no-arg constructor helped solving my problem. – nope Nov 01 '15 at 06:48
  • You're correct. I was wondering even after using jsonlint.com why my JSON was invalid. – Dr4gon Oct 10 '16 at 12:15
  • 2
    Hi! please I have the same problem even if I specify Content-Type in the client side. Can you help me please? – Ne AS Dec 28 '16 at 18:08
  • @Llg please check my answer, and other people's answers too. There is plenty of explanations out there, and I am pretty sure that there are no gremlins or elves working behind Your back ;) – Aleksandar Feb 06 '17 at 22:15
4

Spring provides out-of-box many default HttpMessageConverters, which will be used for conversion, depending on presence of certain library in project classpath.

For example, if the Content-Type in request Header was one of application/json or application/xml , that means the POST body contains json or XML[Popular formats], and if Jackson library is found in your classpath, Spring will delegate the conversion to MappingJackson2HttpMessageConverter [for json] or MappingJackson2XmlHttpMessageConverter [for xml].

To declare a dependency to Jackson library (jackson-databind) include following dependency in your pom.xml

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson.version}</version>
</dependency>
Pradeep
  • 12,309
  • 3
  • 20
  • 25
1

add "annotation-driven" line to your xml file or add content-type as application/json

BHARATHWAJ
  • 39
  • 1
  • 10
0

I too had the same problem. Add ContentType header to application/xml (or whatever ur service is expecting) to the postman.

ABHIJITH GM
  • 134
  • 4
0

I'm a bit shamed to write this, but I forgot to put the enctype="multipart/form-data" attribute in the form tag:

<form action="rest/upload_function_path" method="post" enctype="multipart/form-data">
<!-- this thing right  --------------------------------^ there! -->
    <p>
    Your file is: <input type="file" name="fajl" />
    </p>
    <input type="submit" value="Upload It" />
</form>

This is a pretty lame error, but also the one which took some time to realize.

Aleksandar
  • 3,558
  • 1
  • 39
  • 42