1

Frontend todo.ts and todo.html enter image description here enter image description here

Console.log

enter image description here

Postman enter image description here

backend-code with spring boot //TodoService; function for Update

   public Todos updateTodoWithMap(Long id, Map<Object, Object> objectMap) {

        Optional<Todos> todos = todoRepository.findById(id);

        objectMap.forEach((key, value) -> {
            Field field = ReflectionUtils.findField(Todos.class, (String) key);
            field.setAccessible(true);
            ReflectionUtils.setField(field, todos.get(), value);
        });

        return todoRepository.save(todos.get());
    }

and frontend with angular http request

  updateTodo(id: any, todoData: Todo): Observable<Todo> {
    return this.http.patch<Todo>(`${this.baseUrl}/${id}`, todoData)
      .pipe(
        catchError(this.handleError)
      );
  }

I want to send an http patch request to the server, but I get error message 415. what should I do. what is wrong?

Conroller Class from Todo with PatchMapping:

@CrossOrigin(origins = "https://localhost:8100")
@RestController
@RequestMapping("/auth/users")

public class TodosController {

@Autowired
private final TodosService todoService;


public TodosController(TodosService todoService) {
    this.todoService = todoService;
}

@GetMapping("todo")
public List<Todos> getTodos() {
    return this.todoService.getTodos();
}

@PostMapping("todo")
public Todos addTodos(@RequestBody Todos toDos) {
    return this.todoService.addTodo(toDos);
}

/*@PutMapping("todo/{id}")
public Todos update(@PathVariable(value = "id") Long todoId, @RequestBody Todos todosDetail)
        throws NoteNotFoundException {
    Todos updatedTodos = this.todoService.updateTodo(todoId, todosDetail);
    return updatedTodos;
}*/

  @PatchMapping("todo/{id}")
  public Todos updateTodoWithMap(@PathVariable(value="id") Long id, @RequestBody
  Map<Object,Object> objectMap ){    
 return todoService.updateTodoWithMap(id, objectMap); 
  
  }

@DeleteMapping("todo/{id}")
public Boolean delete(@PathVariable Long id) {
    return this.todoService.delete(id);
}

}

ddola
  • 21
  • 2
  • 415 is unsupported media format. Is your Todo object correct? Does it work with Postman? – Flo Dec 28 '22 at 21:41
  • yes it works with postman. – ddola Jan 04 '23 at 19:16
  • can I see the postman request + console log of the `toDos` item in your backend, please? – Flo Jan 05 '23 at 07:22
  • i try again later, I get the error 500. I uploaded a picture of this, see above @Flo – ddola Jan 05 '23 at 12:36
  • Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Can not set java.util.Date field de.beuth.starfishbook.model.Todos.appointmentTime to java.lang.String] with root cause java.lang.IllegalArgumentException: Can not set java.util.Date field de.beuth.starfishbook.model.Todos.appointmentTime to java.lang.String – ddola Jan 07 '23 at 20:42
  • Try to set the Date as string in the same format you do with Postman. – Flo Jan 08 '23 at 10:07

2 Answers2

0

What is baseUrl set to? If it doesn't contain 'todo' you need to add that in the url like this:

this.http.patch<Todo>(`${this.baseUrl}/todo/${id}`
Johan Nordlinder
  • 1,672
  • 1
  • 13
  • 17
0

First I see you expect a reponse of an array of todos, right? So change your frontend like this:

return this.http.patch<Todo[]>(`${this.baseUrl}/${id}`, todoData)
      .pipe(
        catchError(this.handleError)
      );

Then the error says cannot set java.util.Dateto java.lang.String. So the date was the problem. I have nothing to do with Spring Boot but here i found your problem:

You have correctly set pattern in the Controller Class with the annotation @DateTimeFormat(pattern = "yyyy-MM-dd"). And please also make sure that you have: imported two required patterns in your Model/Entity Class as:

import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Temporal(TemporalType.DATE)
private Date date;
Flo
  • 2,232
  • 2
  • 11
  • 18