Volodymyr Burmus
Volodymyr Burmus

Reputation: 17

Spring app returning HTTP 415 Unsupported Media Type

I'm making SpringMVC webapp. I have a Controller:

@RestController
@RequestMapping(value ="/certificate", produces = MediaType.APPLICATION_JSON_VALUE)
public class GiftCertificateController {
    private final GiftCertificateService gcs;
    
    @Autowired
    public GiftCertificateController(GiftCertificateService gcs) {
        this.gcs = gcs;
    }
    
    @PostMapping
    public ResponseEntity<?> createCertificate(@RequestBody GiftCertificate gc) throws Exception {
        gcs.createCertificate(gc);
        return new ResponseEntity<>(Map.of("status", HttpStatus.CREATED), HttpStatus.CREATED);
    }
    
    // some GetMapping and DeleteMapping functions
    // omitted for simplicity
}

And I am trying to make a POST in Postman to create a certificate with JSON:

{
    "name": "sas",
    "description": "sasasas",
    "price": 12,
    "duration": 12
}

I tried to change my Content-type to application/json, but it still isn't work.

Upvotes: 0

Views: 4346

Answers (3)

Ravi Teja
Ravi Teja

Reputation: 21

I have also faced the issue even after changing the Content-type to application/JSON

  • to resolve it I have created a DTO to take the input from the JSON file
  • and then passed the values to the entity object through the DTO

Upvotes: 0

ABHISHEK PANDEY
ABHISHEK PANDEY

Reputation: 11

​​The error 415-The unsupported file format error occurs when server refuses to accept the request because the payload format is in an unsupported format of file type.

1.Ensure that you are sending the proper Content-Type header value.

2.Verify that your server is able to process the value defined in the Content-Type header.

3.Check the Accept header to verify what the server is actually willing to process.

  • To resolve this issue, explicitly set the content type under the request headers as Post

  • Make sure to change body "Text" to "JSON"

Upvotes: 1

Guido
Guido

Reputation: 47665

The request is not being handled by your controller. HTTP 415 means "Unsupported Media Type".

The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format.

The format problem might be due to the request's indicated Content-Type or Content-Encoding, or as a result of inspecting the data directly.

Try adding the header "Content-Type" with value "application/json" to your postman request.

The search engine in Stack Overflow is quite powerful. These questions might also provide helpful info:

Upvotes: 1

Related Questions