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

后端 未结 5 1121
忘掉有多难
忘掉有多难 2021-02-07 03:24

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

相关标签:
5条回答
  • 2021-02-07 03:43

    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>
    
    0 讨论(0)
  • 2021-02-07 03:43

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

    0 讨论(0)
  • 2021-02-07 03:43

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

    0 讨论(0)
  • 2021-02-07 04:02

    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.

    0 讨论(0)
  • 2021-02-07 04:08

    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.

    0 讨论(0)
提交回复
热议问题