gpt4 book ai didi

java - hibernate -orm-panache-resteasy : {"error" :"id to load is required for loading" ,"code":500} running a native build

转载 作者:行者123 更新时间:2023-12-02 01:43:27 28 4
gpt4 key购买 nike

在我尝试使用 hibernate ORM 和 panache 来测试 quarkus 并使用小型休息服务时,在构建 native 可执行文件并在 docker 容器中运行它后立即出现错误。要点是,它在开发模式下完美运行,但在构建 native 模式后就不行了。

我使用的代码是从水果示例中复制的,并更改为人。我在 docker 容器中使用 postgres 数据库。通过 import.sql 使用一些数据初始化数据库如果我在开发模式下运行它,我可以在 http://localhost:8080/person 上发出请求并获取所有人的列表,如果我在 http://localhost:8080/person/2/ 上提出请求,例如我找到了 id 为 2 的人。在 native 构建之后,我在 Docker 容器中运行该服务。通过相同的请求,我获得了所有人的列表,但如果我想获得 ID 为 2 的人,我会收到响应代码 500 并显示错误:{“error”:“加载需要加载id”,“code”: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}

最佳答案

您可以尝试使用 javax.ws.rs.PathParam而不是org.jboss.resteasy.annotations.jaxrs.PathParam

org.jboss.resteasy.annotations.jaxrs.PathParam没有被考虑在内,所以你最终会通过 null到你的方法,Hibernate ORM with Panache 提示当你尝试加载实体时没有提供 id。

我会看看我们是否可以提高这里的可用性,因为我最近遇到了同样的问题。

关于java - hibernate -orm-panache-resteasy : {"error" :"id to load is required for loading" ,"code":500} running a native build,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57475951/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com