Home > database >  Http status 415 Unsupported Media type error with JSON POST
Http status 415 Unsupported Media type error with JSON POST

Time:06-27

I get the HTTP 415 Unsupported Media Type error when I try to send a POST request to the server. Content-Type: application/json does not solve the problem. The get method works fine. I understand that it may be a matter of ManyToOne dependency, but I can't decide how to write a request.

Enity class:

    @Entity
    @Table(name = "director")
    @Data
    public class Director {
        @Id
        @Column
        private int id;
    
        @Column
        private String name;
    
        @Column
        private String surname;
    
        @Column
        private String post;
    
        @Column
        private int numberPhone;
    
        @OneToMany(mappedBy = "directorFromProjectActivities")
        @JsonManagedReference
        private List<ProjectActivities> projectActivitiesList;

RestController:

    @RestController
    @RequestMapping("/api")
    public class RestControllerDirector {
    import org.example.dao.IDao;
    import org.example.entity.Director;
    import org.example.services.IService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    import java.util.List;
        @PostMapping(value = "/director", consumes = {"*/*"})
        public void addDirector(@RequestBody Director director){
            dao.add(director);
        }
    }

JSON I'm sending a postman:

    {
        "id": 100,
        "name": "Alex",
        "surname": "Round",
        "post": "HR",
        "numberPhone": 82171788
    }

dependence jackson:

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.13.1</version>
</dependency>

How is it possible to solve this problem?

CodePudding user response:

the problem is this line

@PostMapping(value = "/director", consumes = {"/"}) when i tried to recreate your situation it went on internal error you should either remove "consumes" entirely or write application/json in it and write that in postman as well

CodePudding user response:

Try consumes="application/*"

I'll prefer to define cosumers data type using

cosumes = MediaType.APPLICATION_JSON

or if it's support multiple type like json and XML by define:

consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON_VALUE}.

See more examples from spring documentation.

Also, it's good practice to return HTTP status code 201 with a body on the successful creation of a resource (Director). The body can be information returned to the client back like id etc. Like

return new ResponseEntity.status(HttpStatus.CREATED).build()

or with body

return new ResponseEntity.status(HttpStatus.CREATED).body(/** your response object here **/).build()

Make sure add produces annotation just consumes on method addDirector.

Here is ResponseEntity and MediaType documentation.

  • Related