在尝试使用带有panache的休眠ORM和小型休息服务测试quarkus之后,在构建本机可执行文件并将其在docker容器中运行后,我得到了一个错误。重点是,它可以完美地在开发人员模式下工作,但在构建本机之后无法正常工作。
我正在使用的代码从水果示例中复制并更改为人员。我在Docker容器中使用了postgres db。通过import.sql使用一些数据初始化数据库 如果我以开发人员模式运行它,那么我可以在http://localhost:8080/person上发出请求,并获取所有人员的列表,例如,如果我在http://localhost:8080/person/2/上进行请求,那么我的ID为2。 本机构建后,我在docker容器中运行该服务。在相同的请求下,我会得到所有人员的列表,但是如果我想获得例如id 2的人员,则会收到响应代码500,并显示以下错误: {“错误”:“加载需要加载的ID”,“代码”:500} 数据库中存在ID为2的人。
import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import javax.json.Json;
import javax.transaction.Transactional;
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.jboss.resteasy.annotations.jaxrs.PathParam;
import io.quarkus.panache.common.Sort;
/**
* PersonResource
*/
@Path("person")
@ApplicationScoped
@Produces("application/json")
@Consumes("application/json")
public class PersonResource {
@GET
public List<Person> get() {
return Person.listAll(Sort.by("lastName"));
}
@GET
@Path("{id}")
public Person getSingle(@PathParam Long id) {
Person entity = Person.findById(id);
if (entity == null) {
throw new WebApplicationException("Person with id of " + id + " does not exist.", 404);
}
return entity;
}
@POST
@Transactional
public Response create(Person person) {
if (person.id != null) {
throw new WebApplicationException("Id was invalidly set on request.", 422);
}
person.persist();
return Response.ok(person).status(201).build();
}
@PUT
@Path("{id}")
@Transactional
public Person update(@PathParam Long id, Person person) {
if (person.firstName == null || person.lastName == null) {
throw new WebApplicationException("First Name or Last Name was not set on request.", 422);
}
Person entity = Person.findById(id);
if (entity == null) {
throw new WebApplicationException("Person with id of " + id + " does not exist.", 404);
}
entity.firstName = person.firstName;
entity.lastName = person.lastName;
return entity;
}
@DELETE
@Path("{id}")
@Transactional
public Response delete(@PathParam Long id) {
Person entity = Person.findById(id);
if (entity == null) {
throw new WebApplicationException("Person with id of " + id + " does not exist.", 404);
}
entity.delete();
return Response.status(204).build();
}
@Provider
public static class ErrorMapper implements ExceptionMapper<Exception> {
@Override
public Response toResponse(Exception exception) {
int code = 500;
if (exception instanceof WebApplicationException) {
code = ((WebApplicationException) exception).getResponse().getStatus();
}
return Response.status(code)
.entity(Json.createObjectBuilder().add("error", exception.getMessage()).add("code", code).build())
.build();
}
}
}
以下是请求和响应:
$ curl -v -X GET 'http://localhost:8080/person'
Note: Unnecessary use of -X or --request, GET is already inferred.
* Uses proxy env variable no_proxy == 'localhost,127.0.0.1,*sulzer.de'
* Trying ::1:8080...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /person HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.65.3
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Connection: keep-alive
< Content-Type: application/json
< Content-Length: 154
< Date: Tue, 13 Aug 2019 09:15:14 GMT
<
* Connection #0 to host localhost left intact
[{"id":2,"firstName":"Muster","lastName":"Maxmann"},{"id":1,"firstName":"Max","lastName":"Mustermann"},{"id":3,"firstName":"Mann","lastName":"Mustermax"}]
$ curl -v -X GET 'http://localhost:8080/person/2'
Note: Unnecessary use of -X or --request, GET is already inferred.
* Uses proxy env variable no_proxy == 'localhost,127.0.0.1,*sulzer.de'
* Trying ::1:8080...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /person/2 HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.65.3
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 500 Internal Server Error
< Connection: keep-alive
< Content-Type: application/json
< Content-Length: 57
< Date: Tue, 13 Aug 2019 09:15:24 GMT
<
* Connection #0 to host localhost left intact
{"error":"id to load is required for loading","code":500}
答案 0 :(得分:0)
您可以尝试使用b = Blog.objects.all()
c= Blog2.objects.all()
a= set(b+c)
而不是javax.ws.rs.PathParam
吗?
未考虑org.jboss.resteasy.annotations.jaxrs.PathParam
,因此您最终将org.jboss.resteasy.annotations.jaxrs.PathParam
传递给方法,并且Panache的Hibernate ORM抱怨在尝试加载实体时没有提供ID。
我将在这里看看是否可以改善可用性,因为我最近遇到了同样的问题。
答案 1 :(得分:0)
如RESTEasy docs中所述,如果您使用的是新注释,则需要使编译器生成参数名称。将此添加到您的pom.xml
:
<properties>
<maven.compiler.parameters>true</maven.compiler.parameters>
</properties>