estoy queriendo hacer un To-Do list usando SpringBoot con Hibernate, y tengo el siguiente problema: cuando quiero hacer un request POST para crear nuevas tareas en la base de datos que el usuario ingresa, obtengo el error 500 a la hora de fijarme en la consola para revisar por qué no me guarda las tareas en la base de datos. Sé y reconozco que esto es porque hay un error del lado del servidor pero no me logro dar cuenta en qué parte me estoy equivocando... esto serian las clases java
package com.cursojava.curso.models;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.*;
@Entity
@Table(name = "tareas")
public class Tarea {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Getter @Setter @Column(name = "id")
private Long id;
@Getter @Setter @Column(name = "tarea")
private String tarea;
public Tarea() {
}
public Tarea(Long id, String tarea) {
this.id = id;
this.tarea = tarea;
}
package com.cursojava.curso.dao;
import com.cursojava.curso.models.Tarea;
import de.mkammerer.argon2.Argon2;
import de.mkammerer.argon2.Argon2Factory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.*;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaDelete;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaUpdate;
import javax.persistence.metamodel.Metamodel;
import java.util.List;
import java.util.Map;
@Repository
@Transactional
public class TareaDaoImpl implements TareaDao {
@PersistenceContext
private EntityManager entityManager;
@Override
public List<Tarea> getTareas() {
String query = "FROM Tarea";
return entityManager.createQuery(query).getResultList();
}
@Override
public void eliminar(Long id) {
Tarea tarea = entityManager.find(Tarea.class, id);
entityManager.remove(tarea);
}
}
package com.cursojava.curso.controllers;
import com.cursojava.curso.dao.TareaDao;
import com.cursojava.curso.models.Tarea;
import de.mkammerer.argon2.Argon2;
import de.mkammerer.argon2.Argon2Factory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
public class TareaController {
@Autowired
private TareaDao tareaDao;
@RequestMapping(value = "api/tareas", method = RequestMethod.POST)
public void registrarTarea(@PathVariable String t) {
Tarea tarea = new Tarea();
tarea.setTarea(t);
tareaDao.registrar(tarea);
}
@RequestMapping(value = "api/tareas", method = RequestMethod.GET)
public List<Tarea> getTareas() {
return tareaDao.getTareas();
}
@RequestMapping(value = "api/tareas/{id}", method = RequestMethod.DELETE)
public void eliminar(@PathVariable Long id) {
tareaDao.eliminar(id);
}
}
y este el codigo javascript
async function registrarTarea() {
let tarea = document.getElementById('txtTarea').value;
const request = await fetch('api/tareas', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
/*JSON.stringify lo que hace es agarrar cualquier
objeto de JavaScript y lo convierte a un string de JSON*/
body: JSON.stringify(tarea)
});
}
STACKTRACE:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< com.cursojava:curso >-------------------------
[INFO] Building curso 0.0.1-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:2.6.0:run (default-cli) > test-compile @ curso >>>
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ curso ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 1878 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ curso ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ curso ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory C:\Users\computadora\Desktop\Escri\curso\curso\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ curso ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< spring-boot-maven-plugin:2.6.0:run (default-cli) < test-compile @ curso <<<
[INFO]
[INFO]
[INFO] --- spring-boot-maven-plugin:2.6.0:run (default-cli) @ curso ---
[INFO] Attaching agents: []
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.0)
2021-11-22 11:09:13.522 INFO 1576 --- [ main] com.cursojava.curso.CursoApplication : Starting CursoApplication using Java 11.0.3 on computadora-PC with PID 1576 (C:\Users\computadora\Desktop\Escri\curso\curso\target\classes started by computadora in C:\Users\computadora\Desktop\Escri\curso\curso)
2021-11-22 11:09:13.522 INFO 1576 --- [ main] com.cursojava.curso.CursoApplication : No active profile set, falling back to default profiles: default
2021-11-22 11:09:14.473 INFO 1576 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-11-22 11:09:14.505 INFO 1576 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 8 ms. Found 0 JPA repository interfaces.
2021-11-22 11:09:15.597 INFO 1576 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-11-22 11:09:15.659 INFO 1576 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-11-22 11:09:15.659 INFO 1576 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.55]
2021-11-22 11:09:15.893 INFO 1576 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-11-22 11:09:15.893 INFO 1576 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2293 ms
2021-11-22 11:09:16.330 INFO 1576 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-11-22 11:09:16.548 INFO 1576 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-11-22 11:09:16.673 INFO 1576 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-11-22 11:09:16.907 INFO 1576 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.1.Final
2021-11-22 11:09:17.469 INFO 1576 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-11-22 11:09:17.671 INFO 1576 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL55Dialect
2021-11-22 11:09:19.450 INFO 1576 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-11-22 11:09:19.465 INFO 1576 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-11-22 11:09:19.606 WARN 1576 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-11-22 11:09:20.183 INFO 1576 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-11-22 11:09:20.199 INFO 1576 --- [ main] com.cursojava.curso.CursoApplication : Started CursoApplication in 7.781 seconds (JVM running for 8.616)
2021-11-22 11:09:51.195 INFO 1576 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-11-22 11:09:51.195 INFO 1576 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-11-22 11:09:51.196 INFO 1576 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
2021-11-22 11:10:26.237 WARN 1576 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingPathVariableException: Required URI template variable 't' for method parameter type String is not present]