I'm getting 405 in console, when I send PUT request.
Here is my ajax call, which is part of bigger function:
triggerPutRequest(id, stringDate, isDone, title)
.then((data) => {
console.log(data);
})
.catch((error) => {
alert(error);
});
function triggerPutRequest(isDone, id, stringDate, title) {
return new Promise((resolve, reject) => {
$.ajax({
type: 'PUT', // http method
url: window.location + "api/tasks", //endpoint on our backend
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: JSON.stringify({
"id": id,
"title": title,
"dueDate": convertDateToString(new Date(datepicker.value)),
"done": isDone,
}),
success: function (data) {
resolve(data)
},
error: function () {
reject("string")
}
});
})
}
In my Spring Boot I have simple Task model object with omitted unrelevant fields:
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private int id;
@Column(name = "due_date", unique = false, nullable = false)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
private LocalDate dueDate;
@NotEmpty(message = "cannot be empty string")
@Column(name = "title", unique = true, nullable = false)
private String title;
@Column(name = "done", unique = false, nullable = false)
private boolean done;
}
My controller class in Spring Boot and endpoint method for updating look like this:
@RestController
@RequestMapping(value="/api/tasks")
public class TaskController {
@PutMapping(value="/", consumes="application/json")
public ResponseEntity<Task> updateTask(@RequestBody Task taskToBeUpdated){
//update object logic
return new ResponseEntity<Task>(newlyAddedTask, HttpStatus.OK);
}
}