sending http request to my server is incorrect

106 Views Asked by At

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);
}

}

2

There are 2 best solutions below

4
Johan Nordlinder On

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}`
1
Flo On

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;