Skip to content

[Java][Spring] Consumes in get method forces a Content-Type #5613

@davidgri

Description

@davidgri
Description

Annotating a GET method in spring with @RequestMapping(consumes= {....}) forces the user to set a Content-Type to the get-request although a HTTP-GET has no body. If no Content-Type is set an error accrues saying:

Content type 'null' not supported

Therefore accessing a GET Rest-Endpoint with a browser like Chrome is not possible, as no Content-Type is set.

Suggest a Fix

to increase the accessibility of the API I would suggest removing the consuming Content-Type in all get methods. The RFC732 says that a request with body should have a Content-Type, a request without can leave out the Content-Type:

A sender that generates a message containing a payload body SHOULD generate a Content-Type header field in that message unless the intended media type of the enclosed representation is unknown to the sender. If a Content-Type header field is not present, the recipient MAY either assume a media type of "application/octet-stream" ([RFC2046], Section 4.5.1) or examine the data to determine its type.

replacing the following code in AbstractJavaCodegen.preprocessSwagger:

String defaultContentType = hasFormParameters ? "application/x-www-form-urlencoded" : "application/json";
String contentType =  operation.getConsumes() == null || operation.getConsumes().isEmpty()
                            ? defaultContentType : operation.getConsumes().get(0);
                    operation.setVendorExtension("x-contentType", contentType);

with:

//only add content-Type if its no a GET-Method
if(!path.getGet().equals(operation)){
String defaultContentType = hasFormParameters ? "application/x-www-form-urlencoded" : "application/json";
String contentType =  operation.getConsumes() == null || operation.getConsumes().isEmpty()
                            ? defaultContentType : operation.getConsumes().get(0);
                    operation.setVendorExtension("x-contentType", contentType);
}

should do the trick.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions