Home > Back-end >  Spring Boot returns 415: Unsupported Media Type on create
Spring Boot returns 415: Unsupported Media Type on create

Time:12-10

I'm new to Spring Boot. I'm trying to create a create endpoint which only expects an object with only a title and body. The Uuid and created_at should be generated. I try to send a POST request with Postman adding the values under Body and form-data. But it returns the following:

{
    "timestamp": "2022-12-09T15:11:02.659 00:00",
    "status": 415,
    "error": "Unsupported Media Type",
    "path": "/tasks"
}

Controller method:

@PostMapping
    public ResponseEntity<?> create(@RequestBody TaskDTO requestTask) {
        System.out.println(requestTask);
        Task newTask = new Task(requestTask.uuid.toString(), requestTask.title, requestTask.body, requestTask.created_at);
        Task task = taskRepository.save(newTask);


        return ResponseEntity
                .ok()
                .body(task);
    }

Task.java

@Entity
@Table(name = "tasks")
public class Task {

    @Id
    @Getter
    @GeneratedValue
    private UUID uuid;

    @Getter
    @Setter
    private String title;

    @Getter
    @Setter
    private String body;

    @Getter
    @Setter
    @GeneratedValue
    private LocalDateTime created_at;

    public Task(UUID uuid, String title, String body, LocalDateTime created_at) {
        this.uuid = uuid;
        this.title = title;
        this.body = body;
        this.created_at = created_at;
    }

    public Task() {

    }
}

TaskDTO.java

@Getter
@Setter
public class TaskDTO {
    public UUID uuid = UUID.randomUUID();
    public String title;
    public String body;
    public LocalDateTime created_at = LocalDateTime.now();
}

CodePudding user response:

  • From postman select body -> select raw -> select json

enter image description here

  • Related