forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This breaks dev mode, and in general is not needed as Quarkus can perform it's own TCCL management when required. It also provides a slight performance boost. Fixes quarkusio#18299 (cherry picked from commit bc2fdfa)
- Loading branch information
1 parent
31502e1
commit 3ea81af
Showing
9 changed files
with
285 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
.../hibernate-reactive/deployment/src/test/java/io/quarkus/hibernate/reactive/dev/Fruit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.quarkus.hibernate.reactive.dev; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.NamedQuery; | ||
import javax.persistence.SequenceGenerator; | ||
import javax.persistence.Table; | ||
|
||
@Entity | ||
@Table(name = "known_fruits") | ||
@NamedQuery(name = "Fruits.findAll", query = "SELECT f FROM Fruit f ORDER BY f.name") | ||
public class Fruit { | ||
|
||
@Id | ||
@SequenceGenerator(name = "fruitsSequence", sequenceName = "known_fruits_id_seq", allocationSize = 1, initialValue = 10) | ||
@GeneratedValue(generator = "fruitsSequence") | ||
private Integer id; | ||
|
||
@Column(length = 40, unique = true) | ||
private String name; | ||
|
||
public Fruit() { | ||
} | ||
|
||
public Fruit(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Fruit{" + id + "," + name + '}'; | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
...ctive/deployment/src/test/java/io/quarkus/hibernate/reactive/dev/FruitMutinyResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package io.quarkus.hibernate.reactive.dev; | ||
|
||
import static javax.ws.rs.core.Response.Status.CREATED; | ||
import static javax.ws.rs.core.Response.Status.NOT_FOUND; | ||
import static javax.ws.rs.core.Response.Status.NO_CONTENT; | ||
|
||
import java.util.List; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.DELETE; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.PUT; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.ext.ExceptionMapper; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
import org.hibernate.reactive.mutiny.Mutiny; | ||
import org.jboss.logging.Logger; | ||
import org.jboss.resteasy.reactive.RestPath; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
|
||
import io.smallrye.mutiny.Uni; | ||
|
||
@Path("fruits") | ||
@ApplicationScoped | ||
@Produces("application/json") | ||
@Consumes("application/json") | ||
public class FruitMutinyResource { | ||
private static final Logger LOGGER = Logger.getLogger(FruitMutinyResource.class); | ||
|
||
@Inject | ||
Mutiny.SessionFactory sf; | ||
|
||
@GET | ||
public Uni<List<Fruit>> get() { | ||
return sf.withTransaction((s, t) -> s | ||
.createNamedQuery("Fruits.findAll", Fruit.class) | ||
.getResultList()); | ||
} | ||
|
||
@GET | ||
@Path("{id}") | ||
public Uni<Fruit> getSingle(@RestPath Integer id) { | ||
return sf.withTransaction((s, t) -> s.find(Fruit.class, id)); | ||
} | ||
|
||
@POST | ||
public Uni<Response> create(Fruit fruit) { | ||
if (fruit == null || fruit.getId() != null) { | ||
throw new WebApplicationException("Id was invalidly set on request.", 422); | ||
} | ||
|
||
return sf.withTransaction((s, t) -> s.persist(fruit)) | ||
.replaceWith(() -> Response.ok(fruit).status(CREATED).build()); | ||
} | ||
|
||
@PUT | ||
@Path("{id}") | ||
public Uni<Response> update(@RestPath Integer id, Fruit fruit) { | ||
if (fruit == null || fruit.getName() == null) { | ||
throw new WebApplicationException("Fruit name was not set on request.", 422); | ||
} | ||
|
||
return sf.withTransaction((s, t) -> s.find(Fruit.class, id) | ||
// If entity exists then update it | ||
.onItem().ifNotNull().invoke(entity -> entity.setName(fruit.getName())) | ||
.onItem().ifNotNull().transform(entity -> Response.ok(entity).build()) | ||
// If entity not found return the appropriate response | ||
.onItem().ifNull() | ||
.continueWith(() -> Response.ok().status(NOT_FOUND).build())); | ||
} | ||
|
||
@DELETE | ||
@Path("{id}") | ||
public Uni<Response> delete(@RestPath Integer id) { | ||
return sf.withTransaction((s, t) -> s.find(Fruit.class, id) | ||
// If entity exists then delete it | ||
.onItem().ifNotNull() | ||
.transformToUni(entity -> s.remove(entity) | ||
.replaceWith(() -> Response.ok().status(NO_CONTENT).build())) | ||
// If entity not found return the appropriate response | ||
.onItem().ifNull().continueWith(() -> Response.ok().status(NOT_FOUND).build())); | ||
} | ||
|
||
/** | ||
* Create a HTTP response from an exception. | ||
* | ||
* Response Example: | ||
* | ||
* <pre> | ||
* HTTP/1.1 422 Unprocessable Entity | ||
* Content-Length: 111 | ||
* Content-Type: application/json | ||
* | ||
* { | ||
* "code": 422, | ||
* "error": "Fruit name was not set on request.", | ||
* "exceptionType": "javax.ws.rs.WebApplicationException" | ||
* } | ||
* </pre> | ||
*/ | ||
@Provider | ||
public static class ErrorMapper implements ExceptionMapper<Exception> { | ||
|
||
@Inject | ||
ObjectMapper objectMapper; | ||
|
||
@Override | ||
public Response toResponse(Exception exception) { | ||
LOGGER.error("Failed to handle request", exception); | ||
|
||
int code = 500; | ||
if (exception instanceof WebApplicationException) { | ||
code = ((WebApplicationException) exception).getResponse().getStatus(); | ||
} | ||
|
||
ObjectNode exceptionJson = objectMapper.createObjectNode(); | ||
exceptionJson.put("exceptionType", exception.getClass().getName()); | ||
exceptionJson.put("code", code); | ||
|
||
if (exception.getMessage() != null) { | ||
exceptionJson.put("error", exception.getMessage()); | ||
} | ||
|
||
return Response.status(code) | ||
.entity(exceptionJson) | ||
.build(); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.