Vamos a guardar unos saludos para luego poder recuperarlos y usarlos para saludar.
Para esto vamos a crear una clase con el saludo :
package com.hexacta.model;
import javax.persistence.*;
import java.util.Objects;
@Entity
public class Greeting {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false)
@Id
private Integer id;
@Column
private String value;
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public Greeting() {}
public Greeting(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Greeting greeting = (Greeting) o;
return Objects.equals(value, greeting.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}
Luego vamos a hacer una clase DAO para acceso a la base de datos :
package com.hexacta.dao;
import com.hexacta.model.Greeting;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
@ApplicationScoped
public class GreetingDAO {
@Inject
private EntityManager em;
public int save(Greeting aGreeting) {
this.em.persist(aGreeting);
return aGreeting.getId();
}
public Greeting get(int id) {
var qr = em.createQuery("from com.hexacta.model.Greeting g " +
"where g.id = ?1");
qr.setParameter(1, id);
return (Greeting) qr.getSingleResult();
}
}
Modificamos el servicio para que permita guardar los saludos :
package com.hexacta;
import com.hexacta.dao.GreetingDAO;
import com.hexacta.model.Greeting;
import javax.transaction.Transactional;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
@ApplicationScoped
public class GreetingServices {
@Inject
private GreetingDAO dao;
public String greeting(String name) {
return "hola " + name;
}
public String greeting(int id,String name) {
Greeting aGreeting = dao.get(id);
if (aGreeting == null) {
return name;
}
return aGreeting.getValue() + " " + name;
}
@Transactional
public int saveGreeting(String greeting) {
Greeting aGreeting = new Greeting(greeting);
return dao.save(aGreeting);
}
}
Y por último vamos a modificar los servicios REST :
package com.hexacta;
import org.jboss.resteasy.annotations.jaxrs.PathParam;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class GreetingResource {
private GreetingServices service;
@Inject
public GreetingResource(GreetingServices service) {
this.service = service;
}
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Hello RESTEasy";
}
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/{name}")
public String hello(@PathParam String name) {
return this.service.greeting(name);
}
@POST
@Path("/save/{greeting}")
public int saveGreeting(@PathParam String greeting) {
return this.service.saveGreeting(greeting);
}
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/{id}/{name}")
public String hello(@PathParam int id,@PathParam String name) {
return this.service.greeting(id, name);
}
}
Y tengo que configurar h2 que es la base que estamos usando, en el application.properties :
# datasource configuration
quarkus.datasource.db-kind = h2
quarkus.datasource.username = sa
quarkus.datasource.password = a
quarkus.datasource.jdbc.url = jdbc:h2:~/quarkus.db
quarkus.hibernate-orm.database.generation=update
Y listo, podemos guardar diferentes saludos y usarlos.
Dejo el link del repo :
https://github.com/emanuelpeg/quarkusExample/