Home > Software design >  Spring app returning HTTP 415 Unsupported Media Type
Spring app returning HTTP 415 Unsupported Media Type

Time:01-10

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.

CodePudding user response:

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:

  • enter image description here

    CodePudding user response:

    You need to check your maven version and your project SDK version

  • Related